I’ve been busy thinking about a problem, and so far I didn’t find a solution to it.

The abstract scenario

On admin page A, I would like to print the value of a (global) variable—in the context of admin page B, though.

The concrete scenario

On a custom user-options page, I would like to list all metaboxes registered for each post type.

Both plugins and the theme can add/remove metaboxes with respect to the current page context ($pagenow). Thus, the $wp_meta_boxes global is set up in that context.


So far, I’ve read into virtually any hook, read a lot on AJAX (the right and the wrong ways), and tried out numerous different approaches I came up with.
Unfortunatly, without luck.

The question

Is it possible to retrieve the value of $wp_meta_boxes on admin page post-new.php?post_type=my_super_duper_post_type, for instance, from another admin page?

If so, how would I go about that?

Can this be done via AJAX?
Or can I trick the plugins/theme into thinking that the current page is post-new.php?post_type=my_super_duper_post_type, for instance, while we’re actually on my options page?

2 Answers
2

Ok, this could be achieved this way, probably.

add_action('add_meta_boxes_my_super_duper_post_type', 'get_metabox_global_ajax', 9999 );
function get_metabox_global_ajax(){
    if( isset($_GET['mgv']) && $_GET['mgv'] == '1' ){
        global $wp_meta_boxes;
        @error_reporting( 0 );
        header( 'Content-type: application/json' );
        die( json_encode( $wp_meta_boxes ));
    }
}

Now, you send a request from your plugin page to post-new.php?post_type=my_super_duper_post_type&mgv=1 through ajax, and use the returned json response of wp_meta_boxeses variables.

Leave a Reply

Your email address will not be published. Required fields are marked *