Support

Account

Home Forums Feature Requests Rules for Cloed fields

Solving

Rules for Cloed fields

  • I have an options page where is have a bunch of fields grouped with tabs.

    Most of these options should be available to editors and admins but I would like to add a tab only visible to admins.

    I think the logical way to do this would be to utilise the new clone field. Make an admin group and set up rules to only show for admins.

    Then clone this group in another group where the rest of the fields are made.

    In other words. Have cloned groups/fields inherit the rules from their initial group.

  • Hi @jursdotme

    The best way to do it would be by registering the fields via PHP code instead. This page should give you more idea about it: https://www.advancedcustomfields.com/resources/register-fields-via-php/.

    With this method, you can utilize the current_user_can() function to check if the current user is an administrator and then add the fields. Here’s an example how to do it:

    if( function_exists('acf_add_local_field_group') ):
    
    // This will be shown to all users
    $field_options = array (
    	'key' => 'group_56d98afb08172',
    	'title' => 'ACF Tab',
    	'fields' => array (
    		array (
    			'key' => 'field_56d98b01c0a1b',
    			'label' => 'Tab 1',
    			'name' => '',
    			'type' => 'tab',
    			'instructions' => '',
    			'required' => 0,
    			'conditional_logic' => 0,
    			'wrapper' => array (
    				'width' => '',
    				'class' => '',
    				'id' => '',
    			),
    			'placement' => 'top',
    			'endpoint' => 0,
    		),
    		array (
    			'key' => 'field_56d98b32c0a1e',
    			'label' => 'Tab 1 Text',
    			'name' => 'tab_1_text',
    			'type' => 'text',
    			'instructions' => '',
    			'required' => 0,
    			'conditional_logic' => 0,
    			'wrapper' => array (
    				'width' => '',
    				'class' => '',
    				'id' => '',
    			),
    			'default_value' => '',
    			'placeholder' => '',
    			'prepend' => '',
    			'append' => '',
    			'maxlength' => '',
    			'readonly' => 0,
    			'disabled' => 0,
    		),
    		array (
    			'key' => 'field_56d98b22c0a1c',
    			'label' => 'Tab 2',
    			'name' => 'tab_2',
    			'type' => 'tab',
    			'instructions' => '',
    			'required' => 0,
    			'conditional_logic' => 0,
    			'wrapper' => array (
    				'width' => '',
    				'class' => '',
    				'id' => '',
    			),
    			'placement' => 'top',
    			'endpoint' => 0,
    		),
    		array (
    			'key' => 'field_56d98b59c0a1f',
    			'label' => 'Tab 2 Text',
    			'name' => 'tab_2_text',
    			'type' => 'text',
    			'instructions' => '',
    			'required' => 0,
    			'conditional_logic' => 0,
    			'wrapper' => array (
    				'width' => '',
    				'class' => '',
    				'id' => '',
    			),
    			'default_value' => '',
    			'placeholder' => '',
    			'prepend' => '',
    			'append' => '',
    			'maxlength' => '',
    			'readonly' => 0,
    			'disabled' => 0,
    		),
    	),
    	'location' => array (
    		array (
    			array (
    				'param' => 'options_page',
    				'operator' => '==',
    				'value' => 'custom-options-page',
    			),
    		),
    	),
    	'menu_order' => 0,
    	'position' => 'normal',
    	'style' => 'default',
    	'label_placement' => 'top',
    	'instruction_placement' => 'label',
    	'hide_on_screen' => '',
    	'active' => 1,
    	'description' => '',
    );
    
    /* Only show the tab for administrator */
    
    if( current_user_can('administrator') ){
        
        // Add the third tab
        $field_options['fields'][] = array (
            'key' => 'field_56d98b23c0a1d',
            'label' => 'Tab 3',
            'name' => 'tab_1_copy_copy',
            'type' => 'tab',
            'instructions' => '',
            'required' => 0,
            'conditional_logic' => 0,
            'wrapper' => array (
                'width' => '',
                'class' => '',
                'id' => '',
            ),
            'placement' => 'top',
            'endpoint' => 0,
        );
        
        // Add the tab contents
        $field_options['fields'][] = array (
            'key' => 'field_56d98b69c0a20',
            'label' => 'Tab 3 Text',
            'name' => 'tab_3_text',
            'type' => 'text',
            'instructions' => '',
            'required' => 0,
            'conditional_logic' => 0,
            'wrapper' => array (
                'width' => '',
                'class' => '',
                'id' => '',
            ),
            'default_value' => '',
            'placeholder' => '',
            'prepend' => '',
            'append' => '',
            'maxlength' => '',
            'readonly' => 0,
            'disabled' => 0,
        );
    }
    
    acf_add_local_field_group($field_options);
    
    endif;

    Please keep in mind that the fields registered via PHP won’t show up on the field group list on the backend.

    I hope this helps 🙂

  • Im sorry but that’s not really the point 🙂

    Sure it can be done with this work around, but then again, so can everything else that ACF does. My point is that when you have access to these rules, they should be respected even if it is a cloned field.

    Of course there are some instances where it wouldn’t make sense, such as having the rules of the parent group cancel out the cloned field group. But i think over all it would be very powerfull.

  • Hi @jursdotme

    In that case, could you please open a new ticket so it can be passed directly to the plugin author? You can open a new ticket here: https://support.advancedcustomfields.com/new-ticket. Also, please don’t forget to explain your request again.

    Thanks 🙂

Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘Rules for Cloed fields’ is closed to new replies.