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".

Share Improve this question edited Jan 3, 2019 at 16:45 RiddleMeThis 3,7878 gold badges21 silver badges30 bronze badges asked Jan 2, 2019 at 19:55 BrettBrett 4145 silver badges25 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 2

Take 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