Define Metas for certain role (Contributor)

Support MB User Meta Define Metas for certain role (Contributor)

Viewing 12 posts - 1 through 12 (of 12 total)
  • Author
    Posts
  • #5007
    socialitysociality
    Participant

    Hello,

    Nice work! Is it possible to define my metas just for a certain role, like a Contributor?

    Thank you in advance.

    #5011
    Anh TranAnh Tran
    Keymaster

    Hi, I will work on the Include Exclude extension for that. Currently, it has "user_role" rule (include/exclude meta boxes for a certain user role), but hasn't worked with MB User Meta yet.

    #5023
    Anh TranAnh Tran
    Keymaster

    Hi, I've just updated the Include Exclude extension to support "user_role" and "user_id" rules. Please try this.

    #5026
    JackkyJackky
    Participant

    Also you can use current_user_can() condition instead 🙂

    #5028
    socialitysociality
    Participant

    Hello,

    What do you have in mind? Something like

    	if ( current_user_can('structure') ) {
    		$meta_boxes[] = array(
    			'title' => __('Structure Info', 'colours-of-a-journey-plugin'),
    			'type'  => 'user',
    			'fields' => array(
    				array(
    					'name' => __( 'Name', 'colours-of-a-journey-plugin' ),
    					'id'   => $prefix . 'structure_name',
    					'type' => 'text',
    				),
    ),
    );
    }

    I am not sure if something like that can work 🙂

    #5029
    socialitysociality
    Participant

    Also,

    Is there a way to make a field as required field?

    Thank you.

    #5032
    Anh TranAnh Tran
    Keymaster

    Using current_user_can only checks for the role of the current user, not the role of the being edited user. The MB Include Exclude extension can handle that.

    To make a field required, you can simply add 'required' => true. Docs here and here.

    #5066
    socialitysociality
    Participant

    Hello,

    I use something like this

    	$meta_boxes[] = array(
    		'title' => __('Structure Info', 'colours-of-a-journey-plugin'),
    		'type'  => 'user',
    		'fields' => array(
    			array(
    				'name' => __( 'Name', 'colours-of-a-journey-plugin' ),
    				'id'   => $prefix . 'structure_name',
    				'type' => 'text',
    				'required'	=> true,				
    			),
    		),
    	);

    The required attribute does not seem to work.

    #5069
    Anh TranAnh Tran
    Keymaster

    Just looked at the issue and found that WordPress doesn't validate the user edit form. So the required attribute doesn't work.

    You need to use Validation rules, like this:

    'validation' => array(
    	'rules'    => array(
    		$prefix . 'structure_name' => array(
    			'required'  => true,
    		),
    	),
    )

    However, this requires the latest development version on Github. I've just added support for user edit form and term edit form in the core.

    #5108
    socialitysociality
    Participant

    Hello,

    Didn't quite understand.

    1. Where should I put this code snippet? Inside the "fields" array?
    2. For the validation to work do I have to use the development version of the plugin or did you make an update for that?

    Thank you.

    #5116
    Anh TranAnh Tran
    Keymaster

    Hi,

    1. You need to put this code in the meta box parameters, for more info, please read this docs:

    https://metabox.io/docs/validation/

    2. The current version of Meta Box only supports validating fields when editing posts/pages. I've made and improvement to support user meta and term meta. That feature is available on Github: https://github.com/rilwis/meta-box. I will update the plugin a little bit later when we fix some other bugs.

    #5139
    socialitysociality
    Participant

    Thank you for the guidance.

    It worked.

Viewing 12 posts - 1 through 12 (of 12 total)
  • The topic ‘Define Metas for certain role (Contributor)’ is closed to new replies.