Your code has a syntax error, extra close-curly in new_menu()
. Your test_sub_menu()
function has all the vars that are used in new_menu()
– but they’re not global or called to from new_menu()
, so they do nothing and the functions do nothing but return undefined errors.
$parent_slug = 'new-menu';
should be instead, for a subpage, the page you want it under. To go under “Pages” you’ll want the slug to be edit.php?post_type=page
.
The following will do what you’re after:
add_action('admin_menu', function () {
$parent_slug = 'edit.php?post_type=page'; // "Pages"
$page_title="My Plugin Submenu Page";
$menu_title="My Plugin Submenu Page";
$capability = 'edit_pages';
$menu_slug = 'myplugin-submenu-page';
$page="myplugin_submenu_page";
add_submenu_page( $parent_slug, $page_title, $menu_title, $capability, $menu_slug, $page);
});
function myplugin_submenu_page(){
echo "<h1>Sub menu page content goes here.</h1>";
}
All documentation and examples is here in add_submenu_page()
If you want to find what the $parent_slug
is, at the start of the above action function you can see WordPress’s admin menu and submenu with something like:
global $menu, $submenu;
echo "<pre>MENU: \n".print_r($menu,true)."</pre>";
echo "<pre>SUB-MENU: \n".print_r($submenu,true)."</pre>";
die;
There you’ll find all the info on the current navigations so you can tap into it.