Support » Plugin: Advanced Custom Fields: Extended » Disable add/delete/move Flexible Content for non-admins

  • Resolved dave.ooo

    (@crossy)


    Is there any way to disable the add/delete/move options for non-admins? I’d like my clients to simply edit the content, but no more than that.

    I think I could remove some of the features with CSS, but if there is a more natural approach i’d like that instead.

    Thanks in advance!

Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Author Konrad Chmielewski

    (@hwk-fr)

    Hello!

    Yes you can! There are two filters for this purpose:

    – Lock layouts (remove drag & drop)
    – Remove Actions (remove add/remove layout buttons)

    When combined together you can completely lock a flexible content. In your case, you can add the following code in your functions.php. Remember to replace the /name=my_flexible part with the name of your flexible content.

    
    add_filter('acfe/flexible/lock/name=my_flexible', 'my_acf_flexible_lock_user', 10, 2);
    add_filter('acfe/flexible/remove_actions/name=my_flexible', 'my_acf_flexible_lock_user', 10, 2);
    function my_acf_flexible_lock_user($lock, $field){
        
        // Bail early if current user is an administrator
        if(current_user_can('administrator'))
            return $lock;
        
        $lock = true;
        
        return $lock;
        
    }
    

    In this example, the flexible content named my_flexible will be completely locked for non-admins. You can add more conditions if you want, you just have to return true; to activate it!

    Regards.

    Thread Starter dave.ooo

    (@crossy)

    Marvelous!

    It works, but all that remains is the clone and copy icons, can those be hidden as well?

    Best,
    Dave

    Plugin Author Konrad Chmielewski

    (@hwk-fr)

    Hello,

    Yeah, those actions are tied to the “Copy/Paste” setting in the Flexible Content field. You can disable it programmatically based on a condition using the following code:

    
    add_filter('acf/prepare_field/name=my_flexible', 'my_acf_flexible_remove_copy_paste');
    function my_acf_flexible_remove_copy_paste($field){
        
        // Bail early if current user is an administrator
        if(current_user_can('administrator'))
            return $field;
        
        $field['acfe_flexible_copy_paste'] = false;
        
        return $field;
        
    }
    

    I’ll add it to the remove_actions filter in the next update, so you don’t have to add an additional code for this. I added it to the Trello Board: https://trello.com/b/QEgpU7CL/acf-extended

    Hope it helps 🙂

    Regards.

    Thread Starter dave.ooo

    (@crossy)

    Thanks, I think adding it in the filter is good (otherwise the filters usability is questionable).

    For anyone reading alone, I’ve hidden the fields for non admins with the following code (in functions.php):

    add_action('admin_head', 'afce_custom_admin_css');
    function acfe_custom_admin_css() {
        // Only if user is not admin
        if(!current_user_can('administrator')) {
    
        	// Hide clone & move ACF-E
        	echo '<style>
    		    .acf-fc-layout-controls {
    		    	display: none !important;
    		    } 
    	      </style>';
        };
    }
    Plugin Author Konrad Chmielewski

    (@hwk-fr)

    Hey!

    Just FYI, I updated my answer above, which now has a nice code that is use acf/prepare_field instead of CSS 🙂

    Have a nice day!

    Thread Starter dave.ooo

    (@crossy)

    Is that code supposed to work already, or starting next update?

    I added the …_remove_copy_paste, but it only seems to remove the ‘paste’ option. ‘Clone’ still has an icon when hovering the element.

    Best,
    Dave

    Plugin Author Konrad Chmielewski

    (@hwk-fr)

    Hello,

    Yes sorry, the “Clone layout” action is not taken into account by the acf/prepare_field hook. It will be fixed in the next patch, you will just have to use remove_actions filter, and it will disable all possible actions.

    Sorry for the inconvenience, I’ll let you know as soon as the patch is up!

    Regards.

    Hi hwk-fr

    Is it also possible to remove the “edit” and “remove” option for a link-field?
    Also the “add layout” for non-admins?

    best regards
    matti

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Disable add/delete/move Flexible Content for non-admins’ is closed to new replies.