How to add a custom item to WordPress Toolbar (example: adding buddyPress Profile menu)

You need to use the "admin_bar_menu" hook to add a new item to WordPress toolbar (dashboard toolbar at the top of a WordPress blog.) For example, I will add a buddyPress Profile link item to WordPress toolbar.

The URL of the buddyPress Profile page can be set via Settings > buddyPress > Pages. You can configure the URL for yourself, but it's simple to use the following code:

bp_loggedin_user_domain()

Please add the following code snippet to your WordPress theme function file (functions.php):

function buddypress_adminbar_profile($wp_admin_bar){
$bpstieurl = bp_loggedin_user_domain();
$args = array(
'id' => 'buddypress-node',
'title' => 'Profile',
'href' => $bpsiteurl,
'meta' => array(
'class' => 'buddypress-node-class'
)
);
$wp_admin_bar->add_node($args);
}

add_action('admin_bar_menu', 'buddypress_adminbar_profile', 75);

Then, the buddyPress Profile item will be added to WordPress Admin Bar as shown below:

buddyPress Profile menu item in WordPress

For your reference, depending on the settings in buddyPress, guest users might also see the toolbar. In this case, it's a good idea to hide the custom buddyPress Profile menu by adding "if ( is_user_logged_in() )".

Reference:

The number in the code "75" refers to the priority of the custom item. The default priorities are as follows:

  • wp_admin_bar_wp_menu - 10
  • wp_admin_bar_my_sites_menu - 20
  • wp_admin_bar_site_menu - 30
  • wp_admin_bar_updates_menu - 40
  • wp_admin_bar_comments_menu - 60
  • wp_admin_bar_new_content_menu - 70
  • wp_admin_bar_edit_menu - 80

Leave a Comment