admin管理员组

文章数量:1122832

I am trying to put three menu items before the Dashboard menu item in admin dashboard. Problem is that Dashboard has menu position 2.

I can manage to put in two menu items before with position 0 and 1, but 2 collides with Dashboard.

So my thought was to move the Dashboard position. Is this possible? Can I change Dashboard menu position from 2 to something else (like 3, 4 or 5)?

Is there a hook for this?

I am trying to put three menu items before the Dashboard menu item in admin dashboard. Problem is that Dashboard has menu position 2.

I can manage to put in two menu items before with position 0 and 1, but 2 collides with Dashboard.

So my thought was to move the Dashboard position. Is this possible? Can I change Dashboard menu position from 2 to something else (like 3, 4 or 5)?

Is there a hook for this?

Share Improve this question asked Nov 21, 2018 at 20:22 joq3joq3 3713 silver badges20 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 1

Yes, we can change position using admin_menu hook

// Hook into the admin_menu action add_action('admin_menu', 'rearrange_admin_menu', 999);

function rearrange_admin_menu() {
    global $menu;

    // Backup original menu
    $original_menu = $menu;

    // Define the new positions for each menu item
    $new_menu = array();

    // Shift default menu items to new positions
    // Example: Moving Dashboard (index.php) to position 3 and Posts (edit.php) to position 4
    $new_menu[3] = $original_menu[2]; // Dashboard
    $new_menu[4] = $original_menu[5]; // Posts

    // Remove the original menu items
    unset($menu[2], $menu[5]);

    // Reindex the remaining menu items to fill in gaps
    $current_position = 5; // Start after the new items

    foreach ($original_menu as $position => $menu_item) {
        if (!isset($new_menu[$current_position]) && !in_array($menu_item, $new_menu)) {
            $new_menu[$current_position] = $menu_item;
            $current_position++;
        }
    }

    // Assign the rearranged menu back to the global $menu variable
    $menu = $new_menu;
}

Go to screen Options and Install plugin Wp Admin UI Customize then you can change any thing in dashboard.

You can reorder admin menu items like this, notice I moved index.php (dashboard) to the bottom.

function my_custom_menu_order($menu_ord) {
    if (!$menu_ord) return true;

    return array(
        'separator1', // First separator
        'edit.php', // Posts
        'upload.php', // Media
        'link-manager.php', // Links
        'edit-comments.php', // Comments
        'edit.php?post_type=page', // Pages
        'separator2', // Second separator
        'themes.php', // Appearance
        'plugins.php', // Plugins
        'users.php', // Users
        'tools.php', // Tools
        'options-general.php', // Settings
        'separator-last', // Last separator
        'index.php', // Dashboard
    );
}

add_filter( 'custom_menu_order', 'my_custom_menu_order', 10, 1 );
add_filter( 'menu_order', 'my_custom_menu_order', 10, 1 );

If you need to move menu items created manually or by a plugin you can get the correct value to add to the array by debugging like so...

function debug_admin_menu_items() {
    echo '<pre>' . print_r( $GLOBALS[ 'menu' ], TRUE) . '</pre>';
}

add_action( 'admin_init', 'debug_admin_menu_items' );

Note: The above code goes in your theme's functions.php

本文标签: customizationChange Dashboard menu position