Home › Forums › Feature Requests › Rules for Cloed fields › Reply To: Rules for Cloed fields
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 🙂
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.