我如何重新订购管理栏项目?

时间:2013-12-09 作者:Jonathan

我有一个插件,可以添加自定义管理栏项目,但我希望它们一直显示在左侧。我知道我可以使用添加和删除菜单项$wp_admin_bar->add_menu() 等等,但如何重新排序?

编辑:看起来管理栏菜单项顺序是在中定义的wp-includes/class-wp-admin-bar.php, 但我不知道如何在我的插件函数中更改这个顺序。

            add_action( \'admin_bar_menu\', \'wp_admin_bar_sidebar_toggle\', 0 );
            add_action( \'admin_bar_menu\', \'wp_admin_bar_wp_menu\', 10 );
            add_action( \'admin_bar_menu\', \'wp_admin_bar_my_sites_menu\', 20 );
            add_action( \'admin_bar_menu\', \'wp_admin_bar_site_menu\', 30 );

2 个回复
SO网友:Diblo Dk

这是一个老问题,但对未来的其他人来说,答案是。

您可以将以下代码添加到functions.php.

function reorder_admin_bar() {
    global $wp_admin_bar;

    // The desired order of identifiers (items)
    $IDs_sequence = array(
        \'wp-logo\',
        \'site-name\',
        \'new-content\',
        \'edit\'
    );

    // Get an array of all the toolbar items on the current
    // page
    $nodes = $wp_admin_bar->get_nodes();

    // Perform recognized identifiers
    foreach ( $IDs_sequence as $id ) {
        if ( ! isset($nodes[$id]) ) continue;

        // This will cause the identifier to act as the last
        // menu item
        $wp_admin_bar->remove_menu($id);
        $wp_admin_bar->add_node($nodes[$id]);

        // Remove the identifier from the list of nodes
        unset($nodes[$id]);
    }

    // Unknown identifiers will be moved to appear after known
    // identifiers
    foreach ( $nodes as $id => &$obj ) {
        // There is no need to organize unknown children
        // identifiers (sub items)
        if ( ! empty($obj->parent) ) continue;

        // This will cause the identifier to act as the last
        // menu item
        $wp_admin_bar->remove_menu($id);
        $wp_admin_bar->add_node($obj);
    }
}
add_action( \'wp_before_admin_bar_render\', \'reorder_admin_bar\');

https://codex.wordpress.org/Class_Reference/WP_Admin_Bar

SO网友:Jeppe
function custom_menu_order($menu_ord) {  
    if (!$menu_ord) return true;  

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

相关推荐