Support » Plugin: CMB2 » Read-only Field for Specific User Role

  • Resolved Maximus

    (@abayomi222)


    How can I make cmb2 metaboxes or specific fields “read-only” for specific user roles?
    Presently with this function `$cmb->add_field( array(
    ‘name’ => ‘Read Only’,
    ‘description’ => ‘The value of this input should be saved somewhere else.’,
    ‘id’ => ‘_jtcmb2_readonly’,
    ‘type’ => ‘text’,
    ‘save_field’ => false, // Otherwise CMB2 will end up removing the value.
    ‘attributes’ => array(
    ‘readonly’ => ‘readonly’,
    ‘disabled’ => ‘disabled’,
    ),
    ) );`
    I can make a field read-only and this will be for all users.
    How can I make all fields in a metabox or specific fields read-only for specific user roles?

Viewing 10 replies - 1 through 10 (of 10 total)
  • Plugin Author Michael Beckwith

    (@tw2113)

    The BenchPresser

    Not an exact answer, but one that may be better fit, with some small modification:

    $attributes['readonly'] = ( current_user_can( 'manage_options' ) ) ? '' : 'readonly';
    $attributes['disabled'] = ( current_user_can( 'manage_options' ) ) ? '' : 'disabled';
    
    $cmb->add_field( array(
    	'name'        => 'Read Only',
    	'description' => 'The value of this input should be saved somewhere else.',
    	'id'          => '_jtcmb2_readonly',
    	'type'        => 'text',
    	'save_field'  => false, // Otherwise CMB2 will end up removing the value.
    	'attributes'  => $attributes,
    ) );
    

    In this version at least, it’d be a case of anyone who can’t manage_options, which is usually just admins, would get the read-only/disabled version. You can change it to whatever capability you’d want them to have to have.

    If you’re for sure wanting to go based off of roles, you could use something like the code at https://kellenmace.com/get-current-users-role-in-wordpress/ to get the current user’s roles(s), and do something like in_array checks for what roles you want to have it available. Only caveat would be that users can have multiple roles, so there’d be extra code to potentially account for that.

    Thank you Michael.

    I tested the above code , it works but it effects the read-only on all users irrespective of their capabilities.

    In other words, those without manage_options capabilities have their fields in read-only mode as well.

    Can you please fix this?

    Plugin Author Michael Beckwith

    (@tw2113)

    The BenchPresser

    Try this version:

    $attributes = array();
    if ( ! current_user_can( 'manage_options' ) ) {
    	$attributes['readonly'] = '1';
    	$attributes['disabled'] = '1';
    }
    

    Turns out I forgot the presence of the attribute at all triggered it. I also changed this to be if CAN NOT manage options, set the indexes and a value

    Thank you Michael, it worked.

    Plugin Author Michael Beckwith

    (@tw2113)

    The BenchPresser

    welcome

    Hello Michael,

    I am trying to make post status in addition with user capability for the read only fields.

    For example,the code below did not work. What I want to achieve is that along with condition with user capability, if the post status is draft, the specific field or metabox will be readonly.

    $attributes = array();
    if ( ! current_user_can( ‘manage_options’ ) ) {

    if ( ‘draft’ == $post->post_status )
    { $attributes[‘readonly’] = ‘1’;
    $attributes[‘disabled’] = ‘1’;
    }
    }

    • This reply was modified 9 months, 3 weeks ago by Maximus.
    Plugin Author Michael Beckwith

    (@tw2113)

    The BenchPresser

    You’re going to need to find a way to get a post object in order for that to work. Even something as simple as global $post may work, but I can’t guarantee at the exact moment. A different way would be to grab the post ID from the url and the $_GET parameters. You could then pass that into something like get_post() and get the whole object that way.

    Plugin Author Justin Sternberg

    (@jtsternberg)

    You can modify the attributes just before the field is shown by using the show_on_cb callback property: https://github.com/CMB2/CMB2/wiki/Field-Parameters#show_on_cb

    $cmb->add_field( array(
    	'show_on_cb' => function( $field ) {
    		if ( ! current_user_can( 'manage_options' ) || 'draft' == get_post_field( 'post_status', $field->object_id ) ) {
    			$field->args['attributes']['disabled'] = true;
    			$field->args['attributes']['readonly'] = true;
    		}
    		return true;
    	},
    	'name'        => 'Read Only',
    	'description' => 'The value of this input should be saved somewhere else.',
    	'id'          => '_jtcmb2_readonly',
    	'type'        => 'text',
    	'save_field'  => false, // Otherwise CMB2 will end up removing the value.
    	'attributes'  => $attributes,
    ) );

    Thank you Justin,

    I tried the code stated above but it makes the field read-only and disabled irrespective of the post status.

    In other words, even with post status such as pending and published, the specific field is still read-only and disabled.

    Can you please fix this?

    It works

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Read-only Field for Specific User Role’ is closed to new replies.