How to access global $wp_meta_boxes variable on front-end?

Is there a way to take the global $wp_meta_boxes variable to be used in the front-end? Currently the $wp_meta_boxes is accessible on specific admin pages only.

I’ve found a similar question about this. Its solution is to get the data via the do_meta_boxes hook, but I would like to access the variable without JS/AJAX, and in a front-end template.

1 Answer
1

I’m afraid you can’t do what you are after – without AJAX, that is.

The problem lies with the fact that all/most of the meta boxes are added in an admin-specific context.

The add_meta_boxes action hook, for instance, is the most common (and appropriate) place to add custom meta boxes. And this very hook is fired in only three wp-admin files/pages.

And even if you think about firing the above action on the front-end by yourself, you can’t get a hold of all those meta boxes that are added in an admin context only.

I, for myself, add custom meta boxes like so:

if ( is_admin() ) {
    add_action( 'add_meta_boxes', 'wpdev_154684_add_my_custom_meta_box' );
}

function wpdev_154684_add_my_custom_meta_box() {

    add_meta_box(
        /* ... */
    );
}

Of course, this is a strong simplification/abstraction of how the code actually looks like.


So, what then?

Since you linked to my question, I think you already found one way of getting admin-specific stuff – from somewhere other than the actual context.

I’m definitely not saying this is the only way. This is just what I came up with (with all the help from Shazzad and G.M.), for my very special context.

Leave a Comment