0

I want to be able to add a metabox to a new submenu page that I have created. I have the following basic code which I was hoping would work but it seems not to:

add_action("init", "my_register_post_types");
function my_register_post_types()
{
    register_post_type("my-cpt", array(
        "label" => "My CPT",
        "public" => true
    ));
}

add_action("admin_menu", "my_add_menu_items");
function my_add_menu_items()
{
    add_submenu_page("edit.php?post_type=my-cpt", "Test Page", "Test Page", "edit_posts", "my-cpt-slug", "display_my_test_page");
    //return value from this is my-cpt_page_my-cpt-slug
}

function display_my_test_page()
{
    do_meta_boxes("my-cpt", "normal", null);
}

add_action("add_meta_boxes", "my_add_metaboxes");
function my_add_metaboxes()
{
    add_meta_box("my-cpt-metabox", "My CPT Metabox", "display_my_metabox", "my-cpt_page_my-cpt-slug", "normal", "core");
}

function display_my_metabox()
{
    echo "Hello";
}

According to http://shibashake.com/wordpress-theme/standard-wordpress-metabox, I should be using the return value of add_submenu_page to be used in the add_meta_box function in place of a CPT slug.

I was hoping that the the metabox would appear on the new "Test Page" but no joy. Any ideas? Am I missing something?

1 Answer 1

1

The add_meta_boxes hooks load in edit-form-advanced.php. Your custom plugin page "test" does not use that file. You need to hook into the "plugin admin page hooks" instead.

Secondly, the third parameter of add_meta_box is just the CPT slug, not the page slug you have.

Here is what you need to change:

add_action("load-my-cpt_page_my-cpt-slug", "my_add_metaboxes");
function my_add_metaboxes()
{
    add_meta_box("my-cpt-metabox", "My CPT Metabox", "display_my_metabox", "my-cpt", "normal", "core");
}
3
  • Asking a little late, the action hook is triggered but my metabox callback doesn't seem to work. Any reasons? Commented Jun 1, 2015 at 14:23
  • @RutwickGangurde : sorry, no idea, but if you would ask a new question and include your code maybe I, or someone, could help.
    – s_ha_dum
    Commented Jun 1, 2015 at 14:36
  • I had to do do_meta_boxes call! Commented Jun 2, 2015 at 11:25

Your Answer

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

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