jeudi 17 mars 2016

Add post types dynamically to ACF Field Group location

I've got a plugin that uses ACF to determine it's settings. There is a field group that should show up on every post type that is selected from an ACF options page.

I have the options page dynamically populating a checkbox field with valid post types from the site using get_post_type(), but now need to figure out how to add the field group location dynamically based on what's checked on the options page.

Here is my function from the plugin to add the field group to post types:

function acf_facebook_pixels() {

if( function_exists('acf_add_local_field_group') ) {
    acf_add_local_field_group(array (
        'key' => 'group_56dd88d589ecf',
        'title' => 'Facebook Standard Events',
        'fields' => array (
            array (
                'key' => 'field_56dd88de2db74',
                'label' => 'Standard Events',
                'name' => 'fb_standard_events',
                'type' => 'checkbox',
                'instructions' => 'Add standard events to your page / post',
                'required' => 0,
                'conditional_logic' => 0,
                'wrapper' => array (
                    'width' => '',
                    'class' => '',
                    'id' => '',
                ),
                'choices' => array (
                    'search' => 'Search',
                    'cart' => 'Add to cart',
                    'wishlist' => 'Add to wishlist',
                    'checkout' => 'Initiate Checkout',
                    'payment' => 'Add Payment Info',
                    'purchase' => 'Make purchase',
                    'lead' => 'Lead',
                    'registration' => 'Complete Registration',
                ),
                'default_value' => array (
                ),
                'layout' => 'vertical',
                'toggle' => 1,
            ),
            array (
                'key' => 'field_56dd8b29b16d1',
                'label' => 'Purchase Value',
                'name' => 'fb_purchase_value',
                'type' => 'radio',
                'instructions' => '',
                'required' => 0,
                'conditional_logic' => array (
                    array (
                        array (
                            'field' => 'field_56dd88de2db74',
                            'operator' => '==',
                            'value' => 'purchase',
                        ),
                    ),
                ),
                'wrapper' => array (
                    'width' => '',
                    'class' => '',
                    'id' => '',
                ),
                'choices' => array (
                    'custom' => 'Custom Value',
                    'woocom' => 'WooCommerce Order Total',
                ),
                'other_choice' => 0,
                'save_other_choice' => 0,
                'default_value' => 'woocom',
                'layout' => 'vertical',
            ),
            array (
                'key' => 'field_56dd8b4db16d2',
                'label' => 'Custom Value',
                'name' => 'fb_custom_value',
                'type' => 'text',
                'instructions' => '',
                'required' => 0,
                'conditional_logic' => array (
                    array (
                        array (
                            'field' => 'field_56dd88de2db74',
                            'operator' => '==',
                            'value' => 'purchase',
                        ),
                        array (
                            'field' => 'field_56dd8b29b16d1',
                            'operator' => '==',
                            'value' => 'custom',
                        ),
                    ),
                ),
                'wrapper' => array (
                    'width' => '',
                    'class' => '',
                    'id' => '',
                ),
                'default_value' => '',
                'placeholder' => '',
                'prepend' => '',
                'append' => '',
                'maxlength' => '',
                'readonly' => 0,
                'disabled' => 0,
            ),
            array (
                'key' => 'field_56df2a5efc58e',
                'label' => 'View Content',
                'name' => 'fb_view_content',
                'type' => 'checkbox',
                'instructions' => 'Check this box to hide the ViewContent standard event',
                'required' => 0,
                'conditional_logic' => 0,
                'wrapper' => array (
                    'width' => '',
                    'class' => '',
                    'id' => '',
                ),
                'choices' => array (
                    'hide' => 'Hide ViewContent',
                ),
                'default_value' => array (
                ),
                'layout' => 'vertical',
                'toggle' => 0,
            ),
        ),
        'location' => array (
            array (
                array (
                    'param' => 'post_type',
                    'operator' => '==',
                    'value' => 'post',
                ),
            ),
            array (
                array (
                    'param' => 'post_type',
                    'operator' => '==',
                    'value' => 'page',
                ),
            ),
        ),
        'menu_order' => 0,
        'position' => 'normal',
        'style' => 'default',
        'label_placement' => 'top',
        'instruction_placement' => 'label',
        'hide_on_screen' => '',
        'active' => 1,
        'description' => '',
    ));
   }
}

add_action('acf/init', 'acf_facebook_pixels');

Currently, it is displayed by default to post and page, but I need it to read off of the selected checkbox field to populate the array.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire