WordPress Admin Bar Moving Links

Unset

Here’s an example of how to unset the comments link if the default status is ‘closed’ (offers 2 different approaches).

/**
 * Disable 'Comments' link if default status is _closed_
 */
function remove_comments() 
{
    $default_comment_status = get_option( 'default_comment_status' );

    if ( $default_comment_status == 'closed' ) 
    {
        remove_action( 'admin_bar_menu', 'wp_admin_bar_comments_menu', 50 );

        // optional solution using the WP_Admin_Bar class from /wp-includes/class-wp-admin-bar.php
        # global $wp_admin_bar;
        # $wp_admin_bar->remove_menu( 'comments' );
    }
    else 
    {
        return;
    }
}
add_action( 'admin_bar_menu', 'remove_comments', 0 );

You can use remove_action calls for any of the following (/wp-includes/class.admin-bar.php – line 182 – 192) to unset the menu items:

function remove_all_admin_bar_items() 
{
    remove_action( 'admin_bar_menu', 'wp_admin_bar_my_account_menu', 10 );
    remove_action( 'admin_bar_menu', 'wp_admin_bar_my_sites_menu', 20 );
    remove_action( 'admin_bar_menu', 'wp_admin_bar_edit_menu', 30 );
    remove_action( 'admin_bar_menu', 'wp_admin_bar_shortlink_menu', 80 );
    remove_action( 'admin_bar_menu', 'wp_admin_bar_updates_menu', 70 );

    if ( !is_network_admin() && !is_user_admin() ) {
        remove_action( 'admin_bar_menu', 'wp_admin_bar_new_content_menu', 40 );
        remove_action( 'admin_bar_menu', 'wp_admin_bar_comments_menu', 50 );
        remove_action( 'admin_bar_menu', 'wp_admin_bar_appearance_menu', 60 );
    }
}
add_action( 'admin_bar_menu', 'remove_all_admin_bar_items', 0 );

Set and re-order

Add the following calls to your functions.php file after unsetting the items. The integer at the end of every add_action call is the new “order”.

function reorder_admin_bar_items()
{
    add_action( 'admin_bar_menu', 'wp_admin_bar_my_account_menu', 100 );
    add_action( 'admin_bar_menu', 'wp_admin_bar_my_sites_menu', 10 );
    add_action( 'admin_bar_menu', 'wp_admin_bar_edit_menu', 40 );
    add_action( 'admin_bar_menu', 'wp_admin_bar_shortlink_menu', 20 );
    add_action( 'admin_bar_menu', 'wp_admin_bar_updates_menu', 50 );

    if ( !is_network_admin() && !is_user_admin() ) {
        add_action( 'admin_bar_menu', 'wp_admin_bar_new_content_menu', 80 );
        add_action( 'admin_bar_menu', 'wp_admin_bar_comments_menu', 170 );
        add_action( 'admin_bar_menu', 'wp_admin_bar_appearance_menu', 40 );
    }
}
add_action( 'admin_bar_menu', 'reorder_admin_bar_items', 20 );

Add a single item & sub menu item

You can also use the add_admin_bar_menus hook to add items to your admin bar.

function parent_menu_item() 
    {
        global $wp_admin_bar;

        // Parent
        $wp_admin_bar->add_menu( 
            array( 
                 'id'       => 'parent_id_name'
                ,'title'    => __( 'Parent Title', 'your_textdomain' )
                ,'href'     => '#'
            ) 
        );
            // Link to dev mode admin page
            $wp_admin_bar->add_menu( 
                array( 
                     'parent'   => 'parent_id_name'
                    ,'id'           => 'child_id_name'
                    ,'title'    => __( 'Child title', 'your_textdomain' )
                    ,'href'     => '#'
                ) 
            );
    }
    add_action( 'add_admin_bar_menus', 'parent_menu_item', 10 );

I can’t think of how to make this guide more complete, but anybody can feel free to edit my answer and add everything that could be useful.

Leave a Comment

tech