Using WordPress ‘admin_bar_init’ PHP action

The admin_bar_init WordPress PHP action fires after the WP_Admin_Bar is initialized, allowing you to modify or add items to the admin bar.

Usage

add_action('admin_bar_init', 'your_custom_function');

function your_custom_function() {
    // your custom code here
}

Parameters

  • No parameters for this action.

More information

See WordPress Developer Resources: admin_bar_init

Examples

This example adds a custom link to the admin bar.

add_action('admin_bar_init', 'add_custom_link_to_admin_bar');

function add_custom_link_to_admin_bar() {
    global $wp_admin_bar;
    $wp_admin_bar->add_menu(array(
        'id' => 'custom_link',
        'title' => 'Custom Link',
        'href' => 'https://example.com/'
    ));
}

Remove the WordPress logo from the admin bar

This example removes the WordPress logo from the admin bar.

add_action('admin_bar_init', 'remove_wp_logo_from_admin_bar');

function remove_wp_logo_from_admin_bar() {
    global $wp_admin_bar;
    $wp_admin_bar->remove_menu('wp-logo');
}

Change the ‘Howdy’ greeting in the admin bar

This example changes the ‘Howdy’ greeting in the admin bar to ‘Welcome’.

add_action('admin_bar_init', 'change_howdy_greeting');

function change_howdy_greeting() {
    global $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 a custom CSS style to the admin bar

This example adds custom CSS styles to the admin bar.

add_action('admin_bar_init', 'add_custom_css_to_admin_bar');

function add_custom_css_to_admin_bar() {
    wp_register_style('custom_admin_bar_css', plugins_url('css/admin-bar.css', __FILE__));
    wp_enqueue_style('custom_admin_bar_css');
}

Reorder admin bar menu items

This example reorders the admin bar menu items, moving the ‘New’ item to the end of the list.

add_action('admin_bar_init', 'reorder_admin_bar_menu_items');

function reorder_admin_bar_menu_items() {
    global $wp_admin_bar;
    $new_item = $wp_admin_bar->get_node('new-content');
    $wp_admin_bar->remove_menu('new-content');
    $wp_admin_bar->add_menu(array(
        'id' => 'new-content',
        'title' => $new_item->title,
        'href' => $new_item->href,
        'parent' => 'top-secondary'
    ));
}