Use rwmb_show comparing user capability and post field
Support › MB Frontend Submission › Use rwmb_show comparing user capability and post fieldResolved
- This topic has 5 replies, 2 voices, and was last updated 2 years, 10 months ago by
Scott Brim.
-
AuthorPosts
-
June 10, 2022 at 2:08 AM #36404
Scott Brim
ParticipantHi. I would like to show a field if and only if the current user has a capability selected by that field (radio button). I'm new with Meta Box so I'm still not sure of putting the pieces together. I think I want something like
add_filter( "rwmb_show_minimum_required_role", function( $show, $meta_box ) { if ( ! current_user_can( <?= rwmb_get_value( 'minimum_required_role' ) ?> ) ) { $show = false; } return $show; }
How close am I? Thank you.
June 10, 2022 at 2:17 AM #36405Scott Brim
ParticipantSorry, I mixed things up there.
add_filter( "rwmb_show_minimum_required_role", function( $show, $meta_box ) { if ( ! current_user_can( rwmb_get_value( 'minimum_required_role' ) ) ) { $show = false; } return $show; }
June 10, 2022 at 12:30 PM #36421Long Nguyen
ModeratorHi Scott,
The filter hook
rwmb_show_{$meta_box_id}
is applied to the field group (meta box), not to a field. And it is executed on load so it won't work if you select a field value to show it immediately.
You can try to use the extension MB Conditional Logic to show a field based on another field value. Please read more on the documentation https://docs.metabox.io/extensions/meta-box-conditional-logic/June 10, 2022 at 6:43 PM #36432Scott Brim
ParticipantThank you. Sorry, but I don't see a way to use Conditional Logic to check against a user attribute. I'm going to simplify my question. In a form, I would like to show the field only if the user is admin or editor.
Apparently conditional logic in the Builder can only check fields in the field group - I see no way to match against a user role/capability. If I create the field group using code, I have more freedom, but even then, https://docs.metabox.io/extensions/meta-box-conditional-logic/#with-code doesn't show a way to compare with User role or capability.
So, in short: is there a way to check user role or capability, either in Conditional Logic or when registering a meta box with code? Alternatively, is there a filter I can use to decide whether or not to show a specific field?
Thank you for your patience. I'm reading and testing as fast as I can to learn. While I wait for your answer I'll investigate template parts and having multiple meta boxes in a form.
June 11, 2022 at 3:09 AM #36446Long Nguyen
ModeratorHi Scott,
In case of including/excluding a field based on the user role, you can try to use the filter
rwmb_normalize_meta_box
orrwmb_normalize_field
https://docs.metabox.io/filters/rwmb-normalize-meta-box/
https://docs.metabox.io/filters/rwmb-normalize-field/June 16, 2022 at 2:28 AM #36515Scott Brim
ParticipantFYI this is resolved, but I ended up using include/exclude. I split my field group in two, converted one of the field groups to PHP, and added an
include
parameter. Then I set the View to generate the form including both field groups. It all makes sense now and it seems easy. I appreciate your patience with someone who is new to MB. -
AuthorPosts
- You must be logged in to reply to this topic.