Define Metas for certain role (Contributor)
Support › MB User Meta › Define Metas for certain role (Contributor)
- This topic has 11 replies, 3 voices, and was last updated 8 years, 2 months ago by
sociality.
-
AuthorPosts
-
February 8, 2017 at 8:57 PM #5007
sociality
ParticipantHello,
Nice work! Is it possible to define my metas just for a certain role, like a Contributor?
Thank you in advance.
February 9, 2017 at 8:32 AM #5011Anh Tran
KeymasterHi, 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.
February 9, 2017 at 6:04 PM #5023Anh Tran
KeymasterHi, I've just updated the Include Exclude extension to support "user_role" and "user_id" rules. Please try this.
February 9, 2017 at 10:36 PM #5026Jackky
ParticipantAlso you can use current_user_can() condition instead 🙂
February 10, 2017 at 6:57 AM #5028sociality
ParticipantHello,
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 🙂
February 10, 2017 at 7:06 AM #5029sociality
ParticipantAlso,
Is there a way to make a field as required field?
Thank you.
February 10, 2017 at 9:03 AM #5032Anh Tran
KeymasterUsing
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.February 13, 2017 at 4:58 AM #5066sociality
ParticipantHello,
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.
February 13, 2017 at 12:06 PM #5069Anh Tran
KeymasterJust 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.
February 15, 2017 at 9:09 PM #5108sociality
ParticipantHello,
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.
February 16, 2017 at 8:36 AM #5116Anh Tran
KeymasterHi,
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.
February 20, 2017 at 4:44 PM #5139sociality
ParticipantThank you for the guidance.
It worked.
-
AuthorPosts
- The topic ‘Define Metas for certain role (Contributor)’ is closed to new replies.