Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KaineLabs/aa0c1080d84952c08f92f24225984dd9 to your computer and use it in GitHub Desktop.
Save KaineLabs/aa0c1080d84952c08f92f24225984dd9 to your computer and use it in GitHub Desktop.
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

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?

@NicoSteyl
Copy link

NicoSteyl 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