最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

plugins - Get page slug in Admin menu

programmeradmin2浏览0评论

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?

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?

Share Improve this question edited Jan 17, 2020 at 8:00 bueltge 17.1k7 gold badges62 silver badges97 bronze badges asked Jan 17, 2020 at 6:43 Jamin GreyJamin Grey 1211 silver badge5 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

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');
}
发布评论

评论列表(0)

  1. 暂无评论