I know how to add a meta box, and I know how to retrieve meta box values. That's not what I need; I just want to clarify that first. What I need is to get meta box labels by id.

meta box labels

I am adding links to user profiles that allow users to reset which meta boxes are hidden by default. This is saved in the user's meta like so:

meta key = "metaboxhidden_dashboard"    
meta_value = "a:6:{i:0;s:21:"dashboard_site_health";i:1;s:19:"dashboard_right_now";i:2;s:18:"dashboard_activity";i:3;s:18:"rg_forms_dashboard";i:4;s:21:"dashboard_quick_press";i:5;s:17:"dashboard_primary";}"

meta key = "metaboxhidden_courses"
meta_value = "a:2:{i:0;s:10:"wpseo_meta";i:1;s:7:"slugdiv";}"

The only part I'm struggling with is retrieving the label of the meta box instead of displaying the meta box id. I cannot find any way to get these labels. Is there a function I'm not aware of to get them by id? Or maybe get all meta boxes and then pull them accordingly?

Thanks in advance.

EDIT: Here is an example of how it is displayed on the user profile currently. enter image description here

  • 2
    if it's the block editor then a lot of those aren't metaboxes but panels that only exist in React and have no presence in the database or PHP. What's the reason you want the label? Keep in mind the label will be different depending on the language used, and panels don't show up in the metabox APIs. You might also find the labels don't exist in the database because metaboxes are meant to be registered at runtime, they aren't saved in the database. This also means that information is only available when the metabox is registered, aka admin screens
    – Tom J Nowell
    Commented Dec 9, 2022 at 18:40
  • @TomJNowell Thanks for responding. I guess that was a bad example. I want to do this both in Gutenberg and class editor. I just realized that in Gutenberg those ones I shared are panels; thank you for clarifying. So I guess I'd like to be able to reset those as well. I'll look into that separately. Regarding the meta boxes, though, I guess that makes sense about them not being registered at runtime. Perhaps this is just not doable. The reason I want the label is so at a glance the user can see which ones are currently hidden and recognize them by label rather than id (see screenshot above).
    – Aristocles
    Commented Dec 9, 2022 at 18:58
  • Doing this for panels is a different question, panels are implemented completely differently from metaboxes, and their open/close status is also stored in a fundamentally different way, so much so that it might even be browser/computer specific. Metaboxes would only be possible in an admin context, and not all metaboxes would be doable as you can only do it after a metabox has been registered and it's best practice to only register metaboxes when the metabox registration hooks run
    – Tom J Nowell
    Commented Dec 11, 2022 at 16:47


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.