我正在尝试将设置菜单添加到插件中,但在该菜单中,我无法获得“页面”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 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
}
我需要将页面slug传递给
do_settings_sections()
, 但我似乎无法获取当前页面的slug。
我试过:
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
我找到的最接近的是
get_current_screen()->id
, 返回:
myplugin_page_my-plugin-SubPage1
这似乎是我的根页面的名称(来自add_menu_page()
), 再加上“页面”,然后是我的子页面。
目前,我只是脱衣myplugin_page_
关闭(共个)get_current_screen()->id
, 但我一定错过了什么明显的东西?
SO网友:Kidkie
一个快速而简单的解决方案是用$_GET[\'page\']
. 获取查询后,不要忘记对其进行一些安全检查。
此外,当我在上面时,要使子菜单项显示在顶层下方,您需要更改add_submenu_page
到你的顶级鼻涕虫。
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\');
}