admin管理员组文章数量:1287942
I would like to add parameter for the submenus, but when I click to the admin page I receive the following message:
You do not have sufficient permissions to access this page.
Code:
add_submenu_page(
'sandbox',
'Sandbox Options',
'Options',
'administrator',
'sandbox_options&tab=4',
'sandbox_options_display'
);
Without &tab=4 everything is okay.
I would like to add parameter for the submenus, but when I click to the admin page I receive the following message:
You do not have sufficient permissions to access this page.
Code:
add_submenu_page(
'sandbox',
'Sandbox Options',
'Options',
'administrator',
'sandbox_options&tab=4',
'sandbox_options_display'
);
Without &tab=4 everything is okay.
Share Improve this question edited Sep 19, 2013 at 15:00 Mayeenul Islam 12.9k21 gold badges85 silver badges169 bronze badges asked Sep 19, 2013 at 13:47 user1452062user1452062 5271 gold badge10 silver badges21 bronze badges3 Answers
Reset to default 4You'd have to manipulate the global $submenu
and modify the link in it. Or use jQuery.
The following example adds a submenu in the Dashboard menu and changes the destination link just after. The submenu page will dump the contents of the global var.
add_action( 'admin_menu', function()
{
add_submenu_page(
'index.php',
'Sandbox Options',
'Options',
'administrator',
'sandbox_options',
function() { global $submenu; var_dump($submenu); }
);
global $submenu;
$submenu['index.php'][11][2] = 'index.php?page=sandbox_options&tab=3';
});
[Update]
The example given, Redux Framework, uses the following technique:
Add a menu page with a slug
example_slug
.Add the submenu pages and use the same slug
example_slug
+&tab=N
.All the menu and submenu pages are rendered with the menu callback. The submenu have
null
callbacks.
Example:
add_submenu_page(
'sandbox',
'Sandbox Options',
'Options',
'add_users',
'sandbox&tab=4',
'__return_null'
);
Basically you need to reconstruct the url, because plugin_basename
will strip your extra query args after you register the page with query args.
Full (working/tested) solution follows:
define( 'PARENT_SLUG', 'index.php' ); // where to put the submenu
define( 'PAGE_SLUG', 'wp190913_options' ); // submenu slug
define( 'EXTRA_ARG', 'tab' );
define( 'EXTRA_ARG_VALUE', 4 );
add_action( 'admin_menu', 'wp190913_add_page', 11 );
add_action( 'admin_menu', 'wp190913_add_page_args', 12 );
/**
* Register submenu page
*/
function wp190913_add_page() {
add_submenu_page(
PARENT_SLUG,
__('wp190913 Options', 'wp190913_textdomain' ),
__('wp190913 Options', 'wp190913_textdomain'),
'manage_options',
PAGE_SLUG,
'wp190913_display_page'
);
}
/**
* Add extra query arg for submenu page
*/
function wp190913_add_page_args() {
global $submenu;
$position = wp190913_search_submenu( PAGE_SLUG, PARENT_SLUG );
// make sure we modify our page
if ( is_int($position) && $submenu[PARENT_SLUG][$position][2] == PAGE_SLUG ) {
// we will recompose the whole url, starting with parent
$submenu[PARENT_SLUG][$position][2] = add_query_arg( 'page', PAGE_SLUG, PARENT_SLUG );
$submenu[PARENT_SLUG][$position][2] = add_query_arg( EXTRA_ARG, EXTRA_ARG_VALUE, $submenu[PARENT_SLUG][$position][2] );
}
}
/**
* Find submenu key in it's parent array.
*
* @param string $page_slug
* @param string $parent_slug
*
* @return null
*/
function wp190913_search_submenu( $page_slug, $parent_slug ) {
global $submenu;
if ( !isset( $submenu[$parent_slug] ) )
return null;
foreach ( $submenu[$parent_slug] as $i => $item ) {
if ( $page_slug == $item[2] ) {
return $i;
}
}
return null;
}
/**
* Submenu page content.
*/
function wp190913_display_page() {
// Do stuff
echo 'wp190913_display_page';
}
I solved it like this:
class Banner{
function __construct(){
add_action('admin_menu', array($this,'adminmenu'), 200);
}
function adminmenu(){
add_submenu_page(
'adrotate',
'AdRotate Pro · ' . __('Banner Tags', 'adrotate-pro'),
__('Banner Tags', 'adrotate-pro'),
'adrotate_ad_manage',
'adrotate-banner-tags',
array($this,'adrotate_banner_tags')
);
}
function adrotate_banner_tags(){
wp_safe_redirect(admin_url('/edit-tags.php?taxonomy=' . TAX_TAGS));
exit;
}
}
new Banner();
本文标签: wp adminHow to create sub menu with a URL parameter
版权声明:本文标题:wp admin - How to create sub menu with a URL parameter? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741262231a2367841.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论