Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

up vote 0 down vote accepted

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");
}
share|improve this answer
    
Fantastic! Thank you very much indeed! :) –  Tim Smith Aug 17 '13 at 18:32

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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