0

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

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');
  • Thanks a lot for this - but question, does this prevent new items being added to the submenu, by plugins for example? ...as wouldn't they need to be added to this function, or is this simply reordering things? – Brett Jan 2 at 22:03
  • Submenu items not included in that list will not show up. You can test this by commenting out one of the default links. I think it would work the same if you a plugin where to create one, depending on when this happens. – RiddleMeThis Jan 2 at 22:05
  • Ahh, not very ideal then as it would break functionality. – Brett Jan 2 at 22:15
  • It would only effect users submenu. You would have to test it if you plan on adding a plug-in that will add a submenu to users – RiddleMeThis Jan 2 at 22:19
  • It's the best option I've seen – RiddleMeThis Jan 2 at 22:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.