Get BuddyPress Notifications Count


I have made the dive. I have gone 7 years now developing with WordPress and finally a project has approached me that requires BuddyPress. I’ve heard good and I’ve heard bad… over all, I’m pumped to work on BuddyPress and become best friends ;P

Today’s tutorial is a quickie. Let me share with you a work around I created to fix an issue I was faced with. Earlier today I spent about 15 minutes scouring the interwebs looking for that magical function that will return the total number of notifications the current BuddyPress user has, I am defeated. Surprisingly, there is no core function that returns this, but I know for a fact this functionality exists! Digging deep into the core of BuddyPress I was able to locate a function has a snippet of just what I was looking for.

We’ll extract out some code found in the bp_members_admin_bar_notifications_menu() function in bp-members > bp-members-adminbar.php. This function has only been tested with version 1.6.1, so beware! If things update in the future, I’ll be sure to back track and update this post ^_^

Create the custom function

First we need to open the functions.php file in the root of our theme. And paste in this code.

function cg_current_user_notification_count() {
    $notifications = bp_core_get_notifications_for_user(bp_loggedin_user_id(), 'object');
    $count = !empty($notifications) ? count($notifications) : 0;

    echo $count;


We first use the BuddyPress core function _bp_core_get_notifications_foruser(). At first glance this function would seem to do just what we need, unfortunately it returns too much. All we are looking for is the total number of notifications. This function actually returns a multidimensional array, separating each notification into its own array for the currently logged in user. We’ll store this in a variable called$notifications for use in the next part.

With our array of notifications, we then set another variable that we’ll count each array in our $notifications variable and store the results in a new variable called $count.

Lastly we echo the results of our counted array!


Now we can just add in our_ _function where we want! I’ll end this quick tip with a bonus too. the below code will use our new function and wrap that in a link to our user profiles so we can view the notifications that are pending ;)

<p><a href="<?php echo bp_core_get_user_domain(bp_loggedin_user_id()); ?>"><?php cg_current_user_notification_count(); ?> - Notifications</a></p>