1

I am trying to get a list of all registered meta boxes in the wordpress admin, specifically the Dashboard widgets.

Now this is easily achieved with the global variable $wp_meta_boxes and does exactly what I need but only when on the dashboard page.

If I am on a custom admin page or a setting page for example, I get nothing. I am trying to get a list of dashboard widgets and give users the option to put them on a different custom admin page / dashboard.

There are a couple of other similar questions to getting these meta boxes and I am aware of the following code that is been given as an answer, however it has the same problem and it returns an empty array if not on the dashboard page:

function get_meta_boxes( $screen = null, $context = 'advanced' ) {
    global $wp_meta_boxes;

    if ( empty( $screen ) )
        $screen = get_current_screen();
    elseif ( is_string( $screen ) )
        $screen = convert_to_screen( $screen );

    $page = $screen->id;

    return $wp_meta_boxes[$page][$context];
}

Anyone have any ideas? Should the $wp_meta_boxes be working and there is something going on with my setup? Or is there another way of doing this?

Any help would be massively appreciated!

6
  • Why do you want to do this? Metaboxes are only registered when the add_meta_boxes action runs, and even then, they might be conditionally added, and, the code that registers them may not work outside of their usual context. If you can provide the context of why you want this and what problem it solves, as well as where/when you're trying to do this, that would be super helpful
    – Tom J Nowell
    Aug 31, 2020 at 19:03
  • I have a plugin that creates a different dashboard page. I am looking for a way to be able to allow users to select meta boxes that would normally display on the default dashboard and output them to the new dashboard. Say for example the site health box.
    – Mark
    Sep 1, 2020 at 10:39
  • Ah so you're not asking about the metaboxes on the post edit screen, you're asking about dashboard meta boxes? That changes the question significantly, you should edit your questions text and title to make it super clear that's what you're after, people will assume you mean metaboxes in the classic editor. As an aside, the proper way to do that is via the screen settings tab at the top
    – Tom J Nowell
    Sep 1, 2020 at 13:52
  • I have added dashboard to the title, I feel the text is quite clear and says “specifically the dashboard meta boxes” in the opening sentence. What do you mean about the screen settings tab?
    – Mark
    Sep 1, 2020 at 14:17
  • Look on the dashboard, there's a tab labelled screen options. If you click on it, every dashboard widget appears with a checkbox ( dashboard widgets are the terminology to use here, not metaboxes ). As an aside, the metaboxes you're trying to control, did you register them? Or a different plugin? There's a very real possibility that what you want cannot be done, if any of those metaboxes conditionally register themselves based on URL parameters then you're stuck unless you modify them ( high probability )
    – Tom J Nowell
    Sep 1, 2020 at 15:30

1 Answer 1

0

The reason for this is that dashboard widgets don't use the metabox API directly, they use an API that wraps around it, and are only registered on wp_dashboard_setup and wp_network_dashboard_setup.

They're also named dashboard widgets rather than metaboxes, so most users will not know what you're referring to if you talk about metaboxes. Dashboard metaboxes could refer to any metabox in WP Admin.

This means on your custom admin page, those hooks don't fire, resulting in the global variable not containing dashboard widgets.

Additionally, the default WP widgets don't display themselves via those hooks, but instead are added in the same function that displays the dashboard itself, wp_dashboard_setup. This function registers the core dashboard widgets, fires the hooks for plugins to add their own, then triggers the metabox API to display them.

So you would need to:

  • call wp_dashboard_setup on your custom pages
  • move your code to the wp_dashboard_setup hook, and make sure it runs last/late
  • add and remove widgets according to the options you set

It may be easier to remove and replace the main dashboard UI completely, the API was never intended to be used for what you're trying to do with it.

Just keep in mind that the screen options UI will not reflect your changes, and might actively interfere with what you're doing.

1
  • Thanks for this, I would mark it as answered but my rep is too low. I had experimented with wp_dashboard_setup() a little yesterday but i think my brain was fried and couldn't get it to work. I have got it to work now though with your answer! Note for anyone else seeing this though you need to include wp-admin/includes/dashboard.php otherwise wp_dashboard_setup() causes a fatal error.
    – Mark
    Sep 1, 2020 at 18:21

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.