Custom Fields
Re-Envisioned
Support MB User Meta Define Metas for certain role (Contributor)

  • Creator
    Topic
  • #5007
    sociality
    Participant

    Hello,

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

    Thank you in advance.

Viewing 10 replies - 1 through 10 (of 11 total)
  • Author
    Replies
  • #5011
    Anh 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 Tran
    Keymaster

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

    #5026
    Jackky
    Participant

    Also you can use current_user_can() condition instead 🙂

    #5028
    sociality
    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
    sociality
    Participant

    Also,

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

    Thank you.

    #5032
    Anh Tran
    Keymaster

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

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

    #5066
    sociality
    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 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
    sociality
    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 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.

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