The add_admin_bar_menus WordPress PHP action fires after menus are added to the admin menu bar.
Usage
add_action('add_admin_bar_menus', 'your_custom_function');
function your_custom_function() {
// your custom code here
}
Parameters
- None
More information
See WordPress Developer Resources: add_admin_bar_menus
Examples
Add a custom link to the admin bar
Add a link to your website’s homepage in the admin bar.
add_action('add_admin_bar_menus', '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' => 'Visit Homepage',
'href' => home_url()
));
}
Remove the WordPress logo from the admin bar
Remove the default WordPress logo from the admin bar.
add_action('add_admin_bar_menus', 'remove_wp_logo_from_admin_bar');
function remove_wp_logo_from_admin_bar() {
global $wp_admin_bar;
$wp_admin_bar->remove_menu('wp-logo');
}
Add a custom search form to the admin bar
Add a search form to the admin bar that searches your website’s content.
add_action('add_admin_bar_menus', 'add_custom_search_form_to_admin_bar');
function add_custom_search_form_to_admin_bar() {
global $wp_admin_bar;
$form = '<form method="get" id="searchform" action="' . home_url('/') . '">
<input type="text" value="" name="s" id="s" placeholder="Search...">
<input type="submit" id="searchsubmit" value="Search">
</form>';
$wp_admin_bar->add_menu( array(
'id' => 'custom_search_form',
'title' => $form,
'href' => false
));
}
Add a dropdown menu to the admin bar
Add a custom dropdown menu with multiple links to the admin bar.
add_action('add_admin_bar_menus', 'add_custom_dropdown_menu_to_admin_bar');
function add_custom_dropdown_menu_to_admin_bar() {
global $wp_admin_bar;
$wp_admin_bar->add_menu( array(
'id' => 'custom_dropdown',
'title' => 'Custom Dropdown'
));
$wp_admin_bar->add_menu( array(
'parent' => 'custom_dropdown',
'id' => 'custom_link1',
'title' => 'Custom Link 1',
'href' => 'https://example.com/link1'
));
$wp_admin_bar->add_menu( array(
'parent' => 'custom_dropdown',
'id' => 'custom_link2',
'title' => 'Custom Link 2',
'href' => 'https://example.com/link2'
));
}
Change the admin bar color
Change the background color of the admin bar to a custom color.
add_action('add_admin_bar_menus', 'change_admin_bar_color');
function change_admin_bar_color() {
echo '<style type="text/css">
#wpadminbar {
background-color: #FF5733;
}
</style>';
}