Support

Account

Home Forums ACF PRO Hide a field in backend if non admin

Solved

Hide a field in backend if non admin

  • I have searched all all over the web but i could not find any reference on how to do this. I would like to hide one ACF field from the WordPress backend if you are not an administrator.

    I have a Field group(User Profile) that has four fields inside it. Profile Picture, Biography, Role, Primary Role. I Would like that the user can edit/see only three of those: Profile Picture, Biography, Role but only admins could edit/see the Primary Role field.

    This all is in the WordPress backend.

    I know i can use the: Show this field group if rule section but this would hide the whole group.

    Is there a way to hook onto something? or some php code that i could use in functions.php file?

  • function my_acf_load_field( $field ) {
     
        return;
    
    }
    add_filter('acf/load_field/name=acf_primary_role', 'my_acf_load_field');

    I found this solution? is this a good way to do it? Of course this code should be wrapped in a if not admin statement

  • No, that is not a good way to hide a field. Not returning the field from the filter is causing several PHP errors that you’re probably not seeing.

    There isn’t any way using PHP or filters that will let you hide an individual field based on the current user.

    If you want to have fields that that user can see and separate fields that only an admin can see then create 2 field groups. Put the fields the user is allowed to edit in the first and set the second field group’s location rules to only allow it to display based on the current user’s role.

  • I was a little afraid that there is no way of doing this. I think that a hook where you could hide files with php would be really nice. Because now i have to add a new Field group that only has one field in it. and if i want to create a second field that only an admin can edit i have to create a new group and a field in side that. because you can’t have a field only show in profile pages only a group of fields :/

    Is there any way that this feature could be added in the future?

  • No, you put all of the fields that only and admin can see all in the same field group. Then an admin will see all the fields they are supposed to see. One field group for the admin, one for the user and the editor.

    You can submit feature requests by submitting a support ticket https://support.advancedcustomfields.com/new-ticket/

  • That’s true but if some fields are shown on the profile pages you have to have a specific group that has those fields. and if some fields are shown else where you have to have a specific group for those or a’m i getting something wrong?

    ahh nice thanks for the link! will do it straight a way

  • another solution would be to export your field groups to PHP and then dynamically add add these extra fields to the group if the user is an admin. With ACF5 your could even export the groups to JSON, read the JSON files and merger the admin fields into one group if if the user is an admin.

    The functionality you’re looking for has been requested a number of times. I’ve been answering questions here for… must be almost 5 years now. It’s probably on the list of things to do, just low on the priority list.

    There is also the possibility of extending ACF with your own conditional logic.

    Personally, I don’t think that a hidden field, or even a disabled or read only field would be very secure on a page that a user can view and edit. Honestly, anyone that knows anything about coding could alter the page they are looking at in their browser and submit valued to fields they should not be able to submit values to. If you want the fields in a single group then then only secure way to do this would be to generate the field group in PHP and not include the fields they shouldn’t be able to edit.

  • You meant something like:

    function my_acf_add_local_field_groups() {
        acf_add_local_field_group(array(
            'key' => 'group_1',
            'title' => 'My Group',
            'fields' => array (
               array('key' => 'field_1', 'label' => 'Sub Title', 'name' => 'sub_title', 'type' => 'text'),
               if(is_admin()){
                    array('key' => 'field_2', 'label' => 'Sub Title Admin', 'name' => 'sub_title', 'type' => 'text')
               }
            ),
            'location' => array (
                array (
                    array (
                        'param' => 'post_type',
                        'operator' => '==',
                        'value' => 'post',
                    ),
                ),
            ),
        ));
    }
    add_action('acf/init', 'my_acf_add_local_field_groups');
  • Yes, something like that. You just add the fields to the group dynamically. I don’t have an example the relates specifically but using the functions in ACF you can dynamically build field groups https://www.advancedcustomfields.com/resources/register-fields-via-php/

  • Thanks John i will try this technique out! i did not even know that you can build fields with php 😀

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

The topic ‘Hide a field in backend if non admin’ is closed to new replies.