admin管理员组文章数量:1122846
I've added a custom link to WP Admin under the Users menu, but trouble is I can't seem to get it to display above the default options. Is this possible?
My code:
// Add sub-menu option to users menu to contractors section (shortcut)
add_action('admin_menu', 'my_add_contractors_submenu', 5);
function my_add_contractors_submenu() {
global $submenu;
$permalink = admin_url('users.php?role=contractor');
$submenu['users.php'][] = array( 'Contractors', 'manage_options', $permalink );
}
Note that I used this method instead of add_submenu_page
as I needed to add the link myself.
I've tried playing around with the priority
parameter in the add_action
but it won't go any higher - I'm trying to get it to display as the second option, just under " All Users".
I've added a custom link to WP Admin under the Users menu, but trouble is I can't seem to get it to display above the default options. Is this possible?
My code:
// Add sub-menu option to users menu to contractors section (shortcut)
add_action('admin_menu', 'my_add_contractors_submenu', 5);
function my_add_contractors_submenu() {
global $submenu;
$permalink = admin_url('users.php?role=contractor');
$submenu['users.php'][] = array( 'Contractors', 'manage_options', $permalink );
}
Note that I used this method instead of add_submenu_page
as I needed to add the link myself.
I've tried playing around with the priority
parameter in the add_action
but it won't go any higher - I'm trying to get it to display as the second option, just under " All Users".
2 Answers
Reset to default 2Take a look at this function.
function my_submenu_order($menu_ord) {
global $submenu;
// echo '<pre>'.print_r($submenu,true).'</pre>';
$arr = array();
$arr[] = $submenu['users.php'][15]; // Your Profile
$arr[] = $submenu['users.php'][10]; // Add New
$arr[] = $submenu['users.php'][5]; // All Users
$submenu['users.php'] = $arr;
return $menu_ord;
}
add_filter('custom_menu_order', 'my_submenu_order');
Add that to your functions.php. It changes the default order of the user's submenu from [5] [10] [15].
Now un-comment the echo and comment out everything else. This will output the menu item positions for everything. Look for your new submenu and add it to the list. So it would be something like this.
function my_submenu_order($menu_ord) {
global $submenu;
// echo '<pre>'.print_r($submenu,true).'</pre>';
$arr = array();
$arr[] = $submenu['users.php'][15];
$arr[] = $submenu['users.php'][##]; // Add New Number
$arr[] = $submenu['users.php'][10];
$arr[] = $submenu['users.php'][5];
$submenu['users.php'] = $arr;
return $menu_ord;
}
add_filter('custom_menu_order', 'my_submenu_order');
EDIT
The above code works fine BUT if you were to add a plugin later that adds links to the Users submenu they wouldn't show up because we are wiping out the original menu and not including the new links (because we don't know what they are).
If that is a concern, instead of wiping out the menu we can add the newly sorted items to the end and remove the originals. See below.
function my_submenu_order($menu_ord) {
global $submenu;
// echo '<pre>'.print_r($submenu,true).'</pre>';
// Build array of newly sorted items
$arr = array();
$arr[] = $submenu['users.php'][15]; // Your Profile
$arr[] = $submenu['users.php'][10]; // Add New
$arr[] = $submenu['users.php'][5]; // All Users
// Remove the originals
unset($submenu['users.php'][15]);
unset($submenu['users.php'][10]);
unset($submenu['users.php'][5]);
// Add newly items to the list
$submenu['users.php'] += $arr;
return $menu_ord;
}
add_filter('custom_menu_order', 'my_submenu_order');
For anyone looking to reorder multiple submenus, I forked RiddleMeThis' answer. It could be really helpful to anyone looking to rearrange the entire admin menu.
add_filter( 'custom_menu_order', '__return_true' );
function reorder_admin_submenu_menus( $menu_ord ) {
global $submenu;
//Enable the next line to temporary reveal the priority orders of the admin menu
//echo '<pre>'.print_r($submenu,true).'</pre>';
$oldorder = [
//add the originally revealed submenu order HERE.
/* Important:
1. Ensure that the parent page order is the same for both $neworder & $oldorder arrays.
2. Keep the format & brackets the same. */
["users.php" => [5, 10, 15]],
["tools.php" => [20, 10, 15, 5, 31, 25, 30]],
["themes.php" => [5, 6, 7, 8, 10, 12]]
];
$neworder = [
//add your preferred submenu order HERE.
["users.php" => [15, 5, 10]],
["tools.php" => [20, 10, 15, 5, 31, 25, 30]],
["themes.php" => [5, 12, 7, 10, 8, 6]]
];
$pages = count($neworder);
for ($i = 0; $i < $pages; $i++){
$newposition = array();
//Add New
foreach ($neworder[$i] as $parent => $ary){
foreach ($ary as $pos){
$newposition[] = $submenu[$parent][$pos];
}
}
// Remove Originals
foreach ($oldorder[$i] as $parent => $ary){
foreach ($ary as $pos){
unset($submenu[$parent][$pos]);
}
}
$submenu[$parent] += $newposition; //the + allows for appended links
}
return $menu_ord;
}
add_filter( 'menu_order', 'reorder_admin_submenu_menus', 10, 1 );
本文标签: phpChange order of custom submenu link in WP Admin
版权声明:本文标题:php - Change order of custom submenu link in WP Admin? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736297787a1930085.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论