Hide certain Advanced Custom Fields from non admins

I’ve used ACF’s “Flexible Content” to create a fairly advanced “page builder” where authors can create a “section” (basically just a wrapping element with a CSS class name and ID) and then add all my flexible content (images, wysiwyg etc) to it.

What I’d like to do is hide some fields for non admins. I don’t want any old editor to be able to go in and change a section’s ID or class names (as it would mess up the layout).

Read More

I’m aware of the “Rules” panel in the ACF admin where you can choose to only display a certain field group to one type of user role, but what I want is that same thing but for individual fields.

It doesn’t appear to be doable from the admin interface, but I’m wondering if someone knows how it could be done from my functions.php file? Perhaps some filter or action I can hook into and disable certain fields based on the current user’s role?

I’ve attached two screenshots showing what I’d like hidden:

I’d like to hide these choices from the “Add row” menu:

enter image description here

And I’d like these panels to be invisible to non admins:

enter image description here

Edit: While we’re at it, I wouldn’t mind hiding individual fields from a repeatable too. You’ll notice a “Modifiers” field in the first screenshot, that too would be nice to hide from non admins. I guess the solution would be pretty much the same for both problems?

Related posts

2 comments

  1. As of ACF 5.0.0 there is an easier way to do this without having to disable the field or output CSS. If you use the acf/prepare_field hook and return false the field will not render.

    <?php
    function so37111468_hide_field( $field ) {
    
      // hide the field if the current user is not able to save options within the admin
      if ( ! current_user_can( 'manage_options' ) ) {
        return false;
      }
    
      return $field;
    }
    
    add_filter( 'acf/prepare_field/key=MYFIELDKEY', 'so37111468_hide_field' );
    ?>
    

    The documentation for that filter can be found here: https://www.advancedcustomfields.com/resources/acf-prepare_field/

  2. I haven’t managed to actually hide the fields, but I have managed to disable them. Unfortunately simply setting them to disabled in the acf/load_field action wasn’t enough to remove them from the dropdown menu so I also added some CSS to the admin page to visually hide them at least. This is good enough seeing as the editors of the site won’t exactly do their best to break it.

    <?php
    /**
     * Hide some "ACF Section" related custom fields
     */
    add_action('acf/load_field', 'sleek_hide_acf_section_fields', 10, 1);
    
    function sleek_hide_acf_section_fields ($field) {
        $hide = array('section_name', 'section_modifiers', 'modifiers');
    
        global $current_user;
    
        if ((isset($field['_name']) and in_array($field['_name'], $hide)) and (is_admin() && is_user_logged_in() && !in_array('administrator', $current_user->roles))) {
            $field['disabled'] = true;
        }
    
        return $field;
    }
    
    add_action('admin_head', 'sleek_hide_acf_section_fields_css');
    
    function sleek_hide_acf_section_fields_css () {
        $hide = array('section_name', 'section_modifiers', 'modifiers');
    
        global $current_user;
    
        if (is_admin() && is_user_logged_in() && !in_array('administrator', $current_user->roles)) {
            echo '<style>';
    
            foreach ($hide as $h) {
                echo 'div.acf-fc-popup a[data-layout="' . $h . '"]{display: none}';
            }
    
            echo '</style>';
        }
    }
    

Comments are closed.