Support Forum
Support › MB User Meta › Define Metas for certain role (Contributor)
Hello,
Nice work! Is it possible to define my metas just for a certain role, like a Contributor?
Thank you in advance.
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.
Hi, I've just updated the Include Exclude extension to support "user_role" and "user_id" rules. Please try this.
Also you can use current_user_can() condition instead 🙂
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 🙂
Also,
Is there a way to make a field as required field?
Thank you.
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.
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.
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.
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.
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.
Thank you for the guidance.
It worked.