Get page slug in Admin menu

I’m trying to add a settings menu to my plugin, but within that menu, I can’t get the ‘page’ slug.

I’m basically doing something like this:

add_action( 'admin_menu', 'MyPlugin_AddAdminMenus' );
function MyPlugin_AddAdminMenus()
{
    add_menu_page('General settings', 'MyPlugin', 'manage_options', 'my-plugin-General', 'render_generic_settings_page');
    add_submenu_page('my-plugin', 'General settings', 'General settings', 'manage_options', 'my-plugin-General');
    add_submenu_page('my-plugin', 'Lead capturing', 'Lead capturing', 'manage_options', 'my-plugin-SubPage1', 'render_generic_settings_page');
    add_submenu_page('my-plugin', 'Toggle features', 'Toggle features', 'manage_options', 'my-plugin-SubPage2', 'render_generic_settings_page');
}


function render_generic_settings_page()
{
    ?>
    <form action='options.php' method='post'>

        <h1><?php echo $GLOBALS['title']; ?></h1>

        <?php
        settings_fields( 'my-plugin' );
        do_settings_sections( '.........' ); //I need 'my-plugin-SubPage1' or 'my-plugin-SubPage2' or 'my-plugin-General'.
        submit_button();
        ?>

    </form>
    <?php
}

I need to pass the page slug to do_settings_sections(), but I can’t seem to get the current page slug.

I tried:

global $wp_the_query;
global $wp_query;
$wp_query->get('page'); //Both 'page' and 'paged'.
$wp_the_query->get('page'); //Both 'page' and 'paged'.
get_query_var('page');

global $post;
$post->post_name;

global $pagenow;
global $post_type;

get_current_screen()->id
get_current_screen()->base
get_current_screen()->parent_base
get_current_screen()->parent_file
get_current_screen()->post_type
get_current_screen()->title
get_current_screen()->taxonomy

The closest I’ve found is get_current_screen()->id, which returns:
myplugin_page_my-plugin-SubPage1

This seems to be my root page’s name (from add_menu_page()), plus ‘page‘, and then my sub-page’s page.

For the present, I’m just stripping myplugin_page_ off of get_current_screen()->id, but I must be missing something obvious?

1 Answer
1

A fast and easy solution is to fetch the slug with $_GET['page']. Don’t forget to do some safety checks on the query after getting it.

Also, while i’m on it, to make the submenu items show up below your top level you need to change the first property of add_submenu_page to your top level slug.

add_action( 'admin_menu', 'MyPlugin_AddAdminMenus' );
function MyPlugin_AddAdminMenus()
{
    add_menu_page('General settings', 'MyPlugin', 'manage_options', 'my-plugin-General', 'render_generic_settings_page');

    add_submenu_page('my-plugin-General', 'General settings', 'General settings', 'manage_options', 'my-plugin-General');
    add_submenu_page('my-plugin-General', 'Lead capturing', 'Lead capturing', 'manage_options', 'my-plugin-SubPage1', 'render_generic_settings_page');
    add_submenu_page('my-plugin-General', 'Toggle features', 'Toggle features', 'manage_options', 'my-plugin-SubPage2', 'render_generic_settings_page');
}

Leave a Comment