Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Add Notifications And Messages Nav Menu Counts.
<?php
/**
* Add Notifications & Messages Nav Menu Counts.
*/
function yzc_notifications_and_messages_nav_menu_count( $items ) {
// Set up Array's.
foreach( $items as $key => $item ) {
// if user logged-in change the Login Page title to Logout.
if ( strcasecmp( $item->title, 'notifications' ) == 0 ) {
// Get user Notifications
$notifications_count = bp_notifications_get_unread_notification_count();
if ( $notifications_count <= 0 ) {
continue;
}
$item->title .= '<span class="yz-nav-count" style="margin-left: 8px; min-width: 30px; height: 30px; display: inline-block; color: #fff; text-align: center; line-height: 30px; background-color: #FFC107; border-radius: 100%;">'. $notifications_count . '</span>';
} elseif( strcasecmp( $item->title, 'messages' ) == 0 ) {
$msgs_count = bp_get_total_unread_messages_count();
if ( $msgs_count <= 0 ) {
continue;
}
$item->title .= '<span class="yz-nav-count" style="margin-left: 8px; min-width: 30px; height: 30px; display: inline-block; color: #fff; text-align: center; line-height: 30px; background-color: #f95e3c; border-radius: 100%;">'. $msgs_count . '</span>';
}
}
return $items;
}
add_filter( 'wp_nav_menu_objects', 'yzc_notifications_and_messages_nav_menu_count', 10 );
@ericmil87
Copy link

ericmil87 commented May 25, 2020

What if I need it as a shortcode? [yz_notifications_count] [yz_messages_count] something like that.

@Gabri3I3
Copy link

Gabri3I3 commented Nov 5, 2020

is there a shortcode for this function?

@DigitalDirectSA
Copy link

DigitalDirectSA commented Mar 31, 2022

For Shortcodes:

<?php
/*
* Notification Icon with Counter Shortcode
*  use [noticon]
*/

function yzc_notification_sc(){
	$notifications_count = bp_notifications_get_unread_notification_count();
	$not_icon = '<i class="fa fa-bell"></i><span class="yz-notification-count-header" style="color: #fff; text-align: center; background-color: #4990ee; border-radius: 100%;">'. $notifications_count . '</span>'; 

	return $not_icon;
}

add_shortcode('noticon','yzc_notification_sc');

/*
* Message Icon with Counter Shortcode
*  use [mesicon]
*/

function yzc_message_sc(){
	$msgs_count = bp_get_total_unread_messages_count();
	$mes_icon = '<i class="fa fa-envelope"></i><span class="yz-message-count-header" style="color: #fff; text-align: center; background-color: #4990ee; border-radius: 100%;">'. $msgs_count . '</span>';

	return $mes_icon;

}

add_shortcode('mesicon','yzc_message_sc');

?>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment