Search code examples
wordpressnotificationsbuddypress

Frontend Buddypress Notifications - Show for Current User Only


I've added a custom slide-out box to show a user's Buddypress notifications on any page.

https://i.sstatic.net/HbB7f.jpg

I'm essentially echoing the template part from the Buddypress plugin Youzify (formerly known as Youzer). Youzify is just an extension to redesign Buddypress so I believe this problem is not necessarily related to that but rather native Buddypress.

I am able to get the Notifications for the current user when I'm on any standard Wordpress page. However, if I'm on a user's profile, it shows THEIR Notifications instead! I know that by default, Admins can actually see all Notifications if they simply add /notifications to any profile slug, but this is happening for standard Users too.

This is the current code:

<?php global $bp; 

if( bp_has_notifications($bp->loggedin_user->id) ) : ?>

    <?php bp_get_template_part( 'members/single/notifications/notifications-loop' ); ?>

<div class="read-all-notifs"><a href="<?php bp_notifications_unread_permalink($bp->loggedin_user->id); ?>"> View All Notifications</a></div>

<?php else : ?>

    <?php bp_get_template_part( 'members/single/notifications/feedback-no-notifications' ); ?>


<?php endif; ?>

I assume it's something to do with the template part and some function that forces the bp_displayed_user ID when on a Buddypress page, so how can I override that to make sure the Notifications in my popup are always for the current user on ANY page? Anyone have any ideas? Thanks.


Solution

  • I figured this out with help from a developer. This is the correct formatting for the initial part:

    <?php if ( is_user_logged_in() && bp_has_notifications( array('user_id' => bp_loggedin_user_id(), 'per_page' => 10, 'search_terms' => false ) ) ) : ?>
    

    The search terms part was added because on any search results page it was showing the 'No Notifications' loop for everyone.