Support

Account

Home Forums Backend Issues (wp-admin) Disable field in backend

Unread

Disable field in backend

  • I’m trying to disable a field in the backend

    By disabled I mean I don’t want a particular user type to be able to edit this field, so when they click on it in the backend they cannot change its value, but other user type would be able to edit its value … and ideally I’d also like to hide this field but thats not too much of a problem as I could just hide the field with CSS

    I’ve read this here which looks exactly like what I need

    https://www.advancedcustomfields.com/resources/acf-prepare_field/

    And here’s my code

    function acf_load_user_level_field_choices( $field ) {
    
    		$user_access_level = !current_user_can('access_gold');
    	
    		if ( $user_access_level == "gold" ){
    	
    			$field['choices'] = array('standard', 'gold');
    			$field['default_value'] = 0;
    			$field['disabled'] = true;
    			$field['hidden'] = true; 
    			$field['readonly'] = true; 
    		} else {
    			$field['choices'] = array('standard', 'gold');
    			$field['default_value'] = 1;
    		}
    	
    		// return the field
    		return $field;
    		
    	}
    	
    add_filter('acf/load_field/name=user_level', 'acf_load_user_level_field_choices');

    You may notice that code is setting a different value of this field depending on the user role – that is actually working correctly so this piece of code is set up correctly but the field is not disabled or hidden in the backend, I try logging in as my test users for the different roles, standard and gold, go to create a new post but I still see this field and I am able to edit it for both the standard and gold users (but I do see its default value change as stated in the above code) … I feel like maybe I’m misunderstanding the use of ‘$field[‘disabled’] = true;’

    I hope my question is clear, if anyone can point me in the right direction I’d be so appreciative! 🙂

    N

Viewing 1 post (of 1 total)

The topic ‘Disable field in backend’ is closed to new replies.