Use rwmb_show comparing user capability and post field

Support MB Frontend Submission Use rwmb_show comparing user capability and post fieldResolved

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #36404
    Scott BrimScott Brim
    Participant

    Hi. 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.

    #36405
    Scott BrimScott Brim
    Participant

    Sorry, 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;
    }
    #36421
    Long NguyenLong Nguyen
    Moderator

    Hi 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/

    #36432
    Scott BrimScott Brim
    Participant

    Thank 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.

    #36446
    Long NguyenLong Nguyen
    Moderator

    Hi Scott,

    In case of including/excluding a field based on the user role, you can try to use the filter rwmb_normalize_meta_box or rwmb_normalize_field
    https://docs.metabox.io/filters/rwmb-normalize-meta-box/
    https://docs.metabox.io/filters/rwmb-normalize-field/

    #36515
    Scott BrimScott Brim
    Participant

    FYI 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.

Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.