0

For some reason my plugin sub page that I have made isn't showing up or working. The plugin activates and shows the top level menu, but not the sub menu. Anyone help? Code below:

<?php

function new_menu(){
    add_menu_page($page_title, $menu_title, $capability, $menu_slug, $function, $icon_url, $position );
    }
    add_submenu_page( $parent_slug, $page_title, $menu_title, $capability, $menu_slug, $function);
}
add_action('admin_menu', 'new_menu');
function test_sub_menu(){
$parent_slug = 'new-menu';
$page_title = 'Sub menu';
$menu_title = 'Sub menu';
$capability = 'edit_pages';
$menu_slug  = 'new-menu';

}
function new_menu_page(){
    ?>
  <h1>Content goes here.</h1>

<?php
}
function test_sub_menu_page(){
    ?>
  <h1>Sub menu page content goes here.</h1>

    <?php
    }
1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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