Custom Fields
Re-Envisioned
Support Meta Box AIO Field loses its value when using rwmb_before hook

  • Creator
    Topic
  • #31099
    Malak
    Participant

    Hi There,

    I am trying to hide metabox fields based on user role. For that I am trying to use rwmb_before action. The code is pasted below.

    The problem that I am getting is that I can remove the field for a specfic user role. However, the hidden field loses its saved value. How can I preserve the value while hiding the fields for specific roles?

    add_action('rwmb_before' , 'ta_hide_metabox_fields', 10, 1);
    
    function ta_hide_metabox_fields($object){
        print "<pre>";
        print_r($object->meta_box['fields']);
        print "</pre>";
    
        foreach ($object->meta_box['fields'] as $idx => $item){
     /*       print "<pre>";
            print_r($idx);
            print "</pre>";
            print "<pre>";
            print_r($item);
            print "</pre>";
    */
            if($item['id'] == 'specialty'){
                unset($object->meta_box['fields'][$idx]);
            }
        }
    }

    Thanks!

Viewing 3 replies - 1 through 3 (of 3 total)
  • Author
    Replies
  • #31105
    Long Nguyen
    Moderator

    Hi,

    You can try to use the field appearance filters, such as rwmb_{$field_id}_outer_html. Then add a custom class to the field for a specific role and hide the field with simple CSS code

    .custom-class {
        display: none;
    }

    Please read more on this documentation https://docs.metabox.io/filters/#rwmb_outer_html

    #31106
    Malak
    Participant

    Hi Long!

    I remember this option is present in the ACF to hide the field without CSS. Is there any chance that this feature be available soon? Because I believe lots of people will benefit from this.

    For security reasons , I can not hide the fields using CSS only. It is necessary to not print some fields in a fieldset at all.

    Regards

    #31110
    Long Nguyen
    Moderator

    Hi,

    Thanks for your feedback.

    I will inform the development team to support excluding a field based on a condition. In case of using coding, you can try to use the filter rwmb_meta_boxes to unset a field. Please get more details here https://docs.metabox.io/edit-meta-boxes/

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