Support

Account

Home Forums Backend Issues (wp-admin) Show fields in backend by logged in user capability

Solved

Show fields in backend by logged in user capability

  • I see that I can have a group of fields show/not show based on the role of the logged in user.

    What is the recommended way to do the same but based on a specific capability instead of a role?

    The following seems to work:-

    function maybe_hide_field($field)
    {
        if( current_user_can('edit_this_field')
            return $field;
        else
            return NULL;
    }
    add_filter( 'acf/load_field/name=some_field', 'maybe_hide_field' );

    but wasn’t sure if returning NULL is acceptable or something that might cause problems for me elsewhere.

    Am I safe doing it this way, or would you recommend a different way to programatically hide a field in admin?

  • Hi @rowatt

    Your code above is perfectly fine and is a smart way to remove a specific field for a given block of logic.

    Instead of doing this field by field, you can also create your own location rules. Location rules are used on a field group to toggle it’s display. Jump over to the docs to see a tutorial on creating your own location rule (one for specific capability)

    Thanks
    E

  • In this particular instance, just hiding a single field is all I need, but I had completely missed that I can set up custom rules, which will certainly come in useful in other situations.

    Thank you!

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

The topic ‘Show fields in backend by logged in user capability’ is closed to new replies.