Support » Plugin: Advanced Custom Fields: Extended » User Role Permissions to view but not edit

  • Resolved Jon Brown

    (@jb510)


    Is there any way to set a custom user meta field such that the user can view the field, but is unable to edit the field?

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

    (@hwk-fr)

    Hello,

    Thanks for the feedback. It looks like you’re trying to set fields as displayed/readonly. It is possible to set those settings for simple ACF fields, such as Text, Textarea etc… But most of complex fields don’t support it for the moment. The ACF developer confirmed that it will be worked in the 5.10 version.

    In the meantime, you can use the following hook to set a field as disabled/readonly:

    add_filter('acf/prepare_field/name=my_field', 'my_acf_field_disabled');
    function my_acf_field_disabled($field){
        
        // Disabled
        $field['disabled'] = true;
        
        // Readonly
        $field['readonly'] = true;
        
        return $field;
        
    }
    

    If the field you’re trying to disable isn’t compatible, you’ll have to write some custom JS code.

    Hope it helps!

    Regards.

    Thread Starter Jon Brown

    (@jb510)

    Thanks @hwk-fr that got me started!

    Not the prettiest but narrowed it down by user role and then applied to two different fields.

    
    /**
     * Disabled Editing of Lot Number field by home owner role
     *
     * @param [type] $field
     * @return array
     */
    function s9_acf_field_disabled($field) {
    
    	$user = wp_get_current_user();
    	if ( ( in_array ( 'administrator', (array) $user->roles ) ) || ( in_array ( 'board_member', (array) $user->roles ) ) ) {
    		return $field;
       } else {
    		// Disabled
    		$field['disabled'] = true;
    		// Readonly
    		$field['readonly'] = true;
    	}
    	return $field;
    }
    add_filter('acf/prepare_field/name=lot_no', 's9_acf_field_disabled');
    add_filter('acf/prepare_field/name=property_address', 's9_acf_field_disabled');
    
    Plugin Author Konrad Chmielewski

    (@hwk-fr)

    Hello,

    I’m glad to see that you made it work. Just to let you know, you’re not forced to set disabled AND readonly to true at the same time. Those two settings are visually very similar, but have different behaviors.

    The disabled attribute will completely remove the field from the $_POST data that is sent during the saving process. Which means you won’t find it when using acf/save_post hook for example.

    The readonly attribute will simply deny users to change a field value. Its value will be sent to $_POST data, so it can be processed as usual. A warning tho, even when a field is readonly, experienced users can still change the field value using the browser console/dev tools.

    Hope it helps!

    Regards.

    Thread Starter Jon Brown

    (@jb510)

    Thanks for the additional tips. I think it’s harmless in this case to use both.

    This is to keep 60+ members of my home owners association from changing their lot number when they update their contact info on the profile page… Pretty sure none of them could figure out dev tools….

    Plugin Author Konrad Chmielewski

    (@hwk-fr)

    Hello,

    Both is alright, just wanted to let you know 🙂

    Have a nice day!

    Regards.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘User Role Permissions to view but not edit’ is closed to new replies.