Last active
February 11, 2020 18:46
-
-
Save dannydickson/1f90392b61e5a6547108d5588ca87432 to your computer and use it in GitHub Desktop.
Add custom link to WP admin bar
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Taken from: | |
// https://premium.wpmudev.org/blog/add-links-wordpress-admin-bar-toolbar/ | |
// Notes added for posterity's sake | |
// Create function: this name add_toolbar_items will be used when we hook | |
// into the admin bar code after the function is defined | |
function add_toolbar_items($admin_bar){ | |
// Creates parent link | |
$admin_bar->add_menu( array( | |
'id' => 'my-item', // this can be anything you choose, make it describe the link you are adding | |
'title' => 'My Item', | |
'href' => '/your-custom-url-or-slug-here', | |
// Optional site_url() parameter for correct URL formatting in multisites with sub directory setups | |
// 'href' => site_url() . '/your-custom-url-or-slug-here', | |
'meta' => array( | |
'title' => __('My Item'), | |
), | |
)); | |
// Creates child link using the parent parameter, which you can see below | |
$admin_bar->add_menu( array( | |
'id' => 'my-sub-item', | |
'parent' => 'my-item', // this is used for creating a sub-item, must match the id of the parent item | |
'title' => 'My Sub Menu Item', | |
'href' => '/your-custom-url-or-slug-here', | |
'meta' => array( | |
'title' => __('My Sub Menu Item'), | |
'target' => '_blank', | |
'class' => 'my_menu_item_class' | |
), | |
)); | |
// Creates a second child link using the parent parameter, which you can see below | |
$admin_bar->add_menu( array( | |
'id' => 'my-second-sub-item', | |
'parent' => 'my-item', // this is used for creating a sub-item, must match the id of the parent item | |
'title' => 'My Second Sub Menu Item', | |
'href' => '/your-custom-url-or-slug-here', | |
'meta' => array( | |
'title' => __('My Second Sub Menu Item'), | |
'target' => '_blank', | |
'class' => 'my_menu_item_class' | |
), | |
)); | |
} | |
// Action that calls our previously defined custom function called add_toolbar_items | |
// in the admin_bar_menu hook that is built into WordPress | |
add_action('admin_bar_menu', 'add_toolbar_items', 100); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This can be added to the functions.php, or split out into a separate file if that is your workflow.