Using WordPress ‘admin_bar_menu’ PHP action

The admin_bar_menu WordPress PHP action is used to add, remove, or manipulate items in the admin bar.

Usage

add_action('admin_bar_menu', 'my_custom_function', 10, 1);

function my_custom_function($wp_admin_bar) {
    // your custom code here
}

Parameters

  • $wp_admin_bar (WP_Admin_Bar) – The WP_Admin_Bar instance, passed by reference.

More information

See WordPress Developer Resources: admin_bar_menu

Examples

Add a link to the admin bar that directs to the custom settings page.

function my_custom_admin_bar_link($wp_admin_bar) {
    $args = array(
        'id'    => 'my_custom_link',
        'title' => 'Custom Settings',
        'href'  => admin_url('options-general.php?page=my_custom_settings'),
    );
    $wp_admin_bar->add_node($args);
}
add_action('admin_bar_menu', 'my_custom_admin_bar_link', 100);

Remove the WordPress logo from the admin bar

Remove the WordPress logo from the admin bar.

function my_remove_wp_logo($wp_admin_bar) {
    $wp_admin_bar->remove_node('wp-logo');
}
add_action('admin_bar_menu', 'my_remove_wp_logo', 999);

Add a custom admin bar group

Add a custom group with multiple links in the admin bar.

function my_custom_admin_bar_group($wp_admin_bar) {
    $wp_admin_bar->add_group(array(
        'id'     => 'my_custom_group',
        'parent' => 'top-secondary',
    ));

    $wp_admin_bar->add_node(array(
        'id'     => 'my_custom_link_1',
        'title'  => 'Link 1',
        'href'   => '#',
        'parent' => 'my_custom_group',
    ));

    $wp_admin_bar->add_node(array(
        'id'     => 'my_custom_link_2',
        'title'  => 'Link 2',
        'href'   => '#',
        'parent' => 'my_custom_group',
    ));
}
add_action('admin_bar_menu', 'my_custom_admin_bar_group', 100);

Modify the existing “New” dropdown menu in the admin bar

Add a custom post type link to the “New” dropdown menu.

function my_custom_new_content($wp_admin_bar) {
    $wp_admin_bar->add_node(array(
        'id'     => 'new-custom-post-type',
        'title'  => 'Custom Post Type',
        'href'   => admin_url('post-new.php?post_type=custom_post_type'),
        'parent' => 'new-content',
    ));
}
add_action('admin_bar_menu', 'my_custom_new_content', 50);

Change the text of an existing admin bar item

Change the text of the “Howdy” greeting in the admin bar.

function my_custom_greeting($wp_admin_bar) {
    $my_account = $wp_admin_bar->get_node('my-account');
    $new_title = str_replace('Howdy,', 'Welcome,', $my_account->title);

    $wp_admin_bar->add_node(array(
        'id'    => 'my-account',
        'title' => $new_title,
    ));
}
add_action('admin_bar_menu', 'my_custom_greeting', 999);