BuddyPress: Unread Notifications Shortcode
Adds a [wpide_bp_notifications] shortcode that shows a bell link with the member's unread BuddyPress notification count, for headers and menus built with blocks.
Opens WPIDE on
Code
if ( ! function_exists( 'buddypress' ) ) {
return;
}
add_shortcode( 'wpide_bp_notifications', function () {
$user_id = bp_loggedin_user_id();
if ( ! $user_id || ! bp_is_active( 'notifications' ) ) {
return '';
}
$count = (int) bp_notifications_get_unread_notification_count( $user_id );
$label = sprintf( _n( '%d unread notification', '%d unread notifications', $count ), $count );
return sprintf(
'<a class="wpide-bp-bell" href="%1$s" aria-label="%2$s"><span aria-hidden="true">🔔</span>%3$s</a>',
esc_url( bp_get_notifications_permalink( $user_id ) ),
esc_attr( $label ),
$count ? '<span class="wpide-bp-bell__count" aria-hidden="true" style="display:inline-block;min-width:1.4em;margin-left:.25em;padding:0 .35em;border-radius:999px;background:#d63638;color:#fff;font-size:.75em;text-align:center">' . esc_html( number_format_i18n( $count ) ) . '</span>' : ''
);
} );
Notes
Shows nothing to visitors. Style the badge with .wpide-bp-bell__count.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.