Skip to:
Content
Pages
Categories
Search
Top
Bottom

Search Results for 'notifications counter'

Viewing 25 results - 26 through 50 (of 75 total)
  • Author
    Search Results
  • #263461
    Florian Bansac
    Participant

    If there are still people looking for help, I have been trying different things today to:
    -add the notifications counter in menu
    -style differently if 0 notifications or 1+
    -target only the main

    It works for me, maybe you can check from here:

    // filter to target only main menu
    add_filter('wp_nav_menu_items','my_nav_menu_notif_counter', 10, 2);
    
    function my_nav_menu_notif_counter($menu, $args) {
    
    $url = bp_core_get_user_domain(bp_loggedin_user_id()) .'notifications/';
    
    // counter to target unread notifications = 0 or 1+
    $bpnotifcount = bp_notifications_get_unread_notification_count( bp_loggedin_user_id() );
    
           // condition: user must be loggedin
           if (!is_user_logged_in())
                    return $menu;
           else
    
                    // condition: only main nav
    		if( $args->theme_location == 'primary' ) {
    
                            // condition: 0 unread notifications
    			if($bpnotifcount == 0) { 
                    	$notif = '<li><a href=" ' .$url. ' ">Notifications <span class="notifmenucount">'. bp_notifications_get_unread_notification_count( bp_loggedin_user_id() ) .'</span></a></li>';
    			} else {
                   		$notif = '<li><a href=" ' .$url. ' ">Notifications <span class="notifmenucount countnot0">'. bp_notifications_get_unread_notification_count( bp_loggedin_user_id() ) .'</span></a></li>';
    			}
    
            		$menu = $menu . $notif;
            		return $menu;
    		}
    
            	else
    			return $menu;
    
    }

    And CSS

    /*Notifications counter in menu*/
    .notifmenucount {
    border-radius: 25%;
    border: 1px solid #ccc;
    color: #888;
    display: inline;
    margin-left: 1px;
    padding: 2px 6px;
    text-align: center;
    vertical-align: center;
    font-size: small;
    }
    .countnot0 {
    color: #ffff00;
    background: #ff0000;
    border: 1px solid transparent;
    }

    πŸ˜‰

    #263459
    Florian Bansac
    Participant

    I tried all sorts of things on this topic today, I ended up with something that looks like it’s working. Maybe you can try it:

    // filter to target only main menu
    add_filter('wp_nav_menu_items','my_nav_menu_notif_counter', 10, 2);
    
    function my_nav_menu_notif_counter($menu, $args) {
    
    $url = bp_core_get_user_domain(bp_loggedin_user_id()) .'notifications/';
    
    // counter to target unread notifications = 0 or 1+
    $bpnotifcount = bp_notifications_get_unread_notification_count( bp_loggedin_user_id() );
    
           // condition: user must be loggedin
           if (!is_user_logged_in())
                    return $menu;
           else
    
                    // condition: only main nav
    		if( $args->theme_location == 'primary' ) {
    
                            // condition: 0 unread notifications
    			if($bpnotifcount == 0) { 
                    	$notif = '<li><a href=" ' .$url. ' ">Notifications <span class="notifmenucount">'. bp_notifications_get_unread_notification_count( bp_loggedin_user_id() ) .'</span></a></li>';
    			} else {
                   		$notif = '<li><a href=" ' .$url. ' ">Notifications <span class="notifmenucount countnot0">'. bp_notifications_get_unread_notification_count( bp_loggedin_user_id() ) .'</span></a></li>';
    			}
    
            		$menu = $menu . $notif;
            		return $menu;
    		}
    
            	else
    			return $menu;
    
    }

    And CSS:

    /*Notifications counter in menu*/
    .notifmenucount {
    border-radius: 25%;
    border: 1px solid #ccc;
    color: #888;
    display: inline;
    margin-left: 1px;
    padding: 2px 6px;
    text-align: center;
    vertical-align: center;
    font-size: small;
    }
    .countnot0 {
    color: #ffff00;
    background: #ff0000;
    border: 1px solid transparent;
    }

    I hope it helps!

    #261057

    In reply to: Bp custom Menu

    livingflame
    Participant

    Hi @danbp
    Me again.

    First look at this:

    // Add Count Near Nav Menu
    function my_counter_nav_menu($menu) {
    
    	if ( ! is_user_logged_in() ) {
    		return $menu;
    	}
    
    	$user_id = bp_loggedin_user_id();
    	$user_url = bp_loggedin_user_domain();
    
    	$notify_url = $user_url . bp_get_notifications_slug() . '/';
    
    	ob_start();
    	?>
    	
    	<li><a href="<?php echo $notify_url ?>">Notificaciones <span class="my_bubble"> <?php echo bp_notifications_get_unread_notification_count( $user_id );?></span> </a></li>
    <?php
    	$menu_items = ob_get_clean();
    
    	$menu = $menu . $menu_items;
    	return $menu;
    }
    add_filter( 'wp_nav_menu_items', 'my_counter_nav_menu' );
    
    

    This is a modification of your code for show notifications bubbles in Main Menu.

    Okey, it works but, I dont want this line: <?php echo $notify_url ?>”>Notificaciones

    I want to get bubbles for Buddypress-Nav-Menu. You know, Dashboard / Menus / Buddypress. Here you have buddypress links: profile, messages, etc. but, if you add “messages”, appear without counter. So, I want counter for these…

    #260826
    Tranny
    Participant

    Hey Scott @redsand

    Thank you for your message. I’m gonna take a look at the plugin you have suggested. I’m still hoping that BP developers will implement basic features that would help in countering spam. Some such features would be:

    1 – Ability to turn off email notifications for all existing users, and choose a default setting for new users
    2 – Ability to set a limit on how many members can be tagged per post
    3 – Ability to set a limit on how many friendship connections can a member make per day, and the ability to set after how many pending friendship requests the member can’t make any more requests
    4 – Ability to set a period in days after registration, during which a newly registered member will not be able to tag anyone or make friendship connections
    5 – Ability to force moderation on BP related posts similar to WP core, whereby a post would be held in moderation unless a user has an approved post

    Features like that would most certainly be welcome by owners of larger sites, and probably by owners of smaller sites too. They can be frequently encountered in other scripts.

    I do realize that fighting spam is a neverending battle, but features I’ve been talking about would make a significant impact on a webmaster’s ability to reduce it, especially since right now BP doesn’t have anything to mitigate it in any way.

    #260083

    In reply to: Bp custom Menu

    livingflame
    Participant

    RESOLVED! Thanks @sbrajesh

    CODE :::::::: Put this Code Inside your function.php ChildTheme

    //Add Count Near Nav Menu With Bubble
    function my_counter_nav_menu($menu) {
    
    	if ( ! is_user_logged_in() ) {
    		return $menu;
    	}
    
    	$user_id = bp_loggedin_user_id();
    	$user_url = bp_loggedin_user_domain();
    
    	$msg_url = $user_url . bp_get_messages_slug() . '/';
    	$notify_url = $user_url . bp_get_notifications_slug() . '/';
    
    	ob_start();
    	?>
    	<li><a>">Messages <span class="my_bubble_notify"><?php echo bp_get_total_unread_messages_count( $user_id );?></span> </a></li>
    	
    	<li><a>">Notifications <span class="my_bubble_notify"> <?php echo bp_notifications_get_unread_notification_count( $user_id );?></span> </a></li>
    <?php
    	$menu_items = ob_get_clean();
    
    	$menu = $menu . $menu_items;
    	return $menu;
    }
    add_filter( 'wp_nav_menu_items', 'my_counter_nav_menu' );

    ––––––––––––
    CSS::::: Put this Code Inside your style.css ChildTheme

    .my_bubble_notify {
    border-radius: 25%;
    border: 1px solid #ccc;
    background: #eee;
    color: #6c6c6c;
    display: inline;
    margin-left: 1px;
    padding: 2px 6px;
    text-align: center;
    vertical-align: center;
    font-size: small;
    }

    #260036

    In reply to: Bp custom Menu

    danbp
    Participant

    The snippets works as expected and show the counters to LOGGED-IN user.

    Check the source code of the items.

    You should have something like this while looged-in:

    <li><a href="..../messages/ ">Messages (0)</a></li>
    <li><a href="..../notifications/ ">Notifications (3)</a></li>

    The count is in brackets.

    If you want a real bubble, you have to style it yourself with css. To do so, you need to add a span around the counter. Exactly like this:
    Notifications<span class="my_bubble">('. bp...blah... .')</span></a>

    #260025
    livingflame
    Participant

    I’m trying this code courtesy of @danbp , works but I can get bubbles!

    I think BP developers should include this in the next update. PLEASE!

    Examples:

    https://goo.gl/NTEoUE

    https://goo.gl/QEUSz5

    https://goo.gl/MUcBrw

    CODE:::::: Location: put in: YourChildTheme / Function.php ::::::

    //Messages and Notifi for Menu
    function my_counter_nav_menu($menu) {  
    
    $msg_url = bp_core_get_user_domain(bp_loggedin_user_id()) .'messages/';
    $notif_url = bp_core_get_user_domain(bp_loggedin_user_id()) .'notifications/';   
          
        if (!is_user_logged_in())
            return $menu;
        else
            $notif = '
    <li><a href=" ' .$msg_url. ' ">Messages ('.  bp_get_total_unread_messages_count( bp_loggedin_user_id() ) .')</a></li>
    <li><a href=" ' .$notif_url. ' ">Notifications ('. bp_notifications_get_unread_notification_count( bp_loggedin_user_id() ) .')</a></li> 
    ';
    				
        $menu = $menu . $notif;
        return $menu;
    }
    add_filter( 'wp_nav_menu_items', 'my_counter_nav_menu' );
    #259884
    livingflame
    Participant

    I’m new with BuddyPress. So, I want to show Notifications, Unread Message, etc. On a Custom Menu. You know, Wp / Appearance / Menus, Here you have BuddyPress to add, but, with out Counter or Bubbles.

    Check ––> https://goo.gl/reojsE

    So PLEASE, Tell Me How I can do it.

    antoinegdln4
    Participant

    Sorry for the double-post. I can’t close the other…and he’s not rightful.
    I actually have this code to display the notifications on header and it works.

    I just want to know how i can get a “count” condition for the notifications, and if it’s more than 1, that’s change the class “pending-count” to “pending-count-alert”…

    Thanks by advance

    function my_nav_menu_notif_counter($menu) {      
            if (!is_user_logged_in())
        
                    return $menu;
            else
                    
                    $notif = '<li ><a class="ab-item" href="' . bp_core_get_user_domain(bp_loggedin_user_id() ) . 'notifications/">'. __('').'<span id="ab-pending-notifications" class="pending-count">'. __(''). bp_notifications_get_unread_notification_count( bp_loggedin_user_id() ) .'</span> </a></li>';
                    $count = !empty( $notif ) ? count( $notif ) : 0;
                    $menu = $menu . $notif;
                    return $menu;
                
                
                
                    
    }
    add_filter( 'wp_nav_menu_items', 'my_nav_menu_notif_counter' );
    add_filter( 'show_admin_bar', '__return_false' );
    
    ?>
    #259813
    antoinegdln4
    Participant

    Hi Everyone !
    One year ago, i try to put the notifications of the Buddypress bar into my header.
    And, thanks to the members of this forum, it works succesfully.
    I have now a new item on my menu, and it’s a count of the notifications.

    But i want to see immediatly when there is a new notification !
    I would like to change the color of the notification bubble only when there is a new (ou more) notification !
    Can you help me ? :/

    There is my actual php code, on the bp-custom.php

    function my_nav_menu_notif_counter($menu) {      
            if (!is_user_logged_in())
                    return $menu;
            else
                    $notif = '<li ><a class="ab-item" href="' . bp_core_get_user_domain(bp_loggedin_user_id() ) . 'notifications/">'. __('').'<span id="ab-pending-notifications" class="pending-count alert">'. __(''). bp_notifications_get_unread_notification_count( bp_loggedin_user_id() ) .'</span> </a></li>';
                    $menu = $menu . $notif;
                    return $menu;
    }
    add_filter( 'wp_nav_menu_items', 'my_nav_menu_notif_counter' );
    add_filter( 'show_admin_bar', '__return_false' );

    Sorry if my english is bad, i’m french
    I have bad knowledges in Php, but i can try to understand the code

    WP 4.4.5
    BP 2.6.2

    #259418
    juuderd
    Participant

    Hi,

    I followed different tutorials to instal buddypress step by step. But still i got the message not found page for the page’s: PROFILE, NOTIFICATIONS, MESSAGES, FRIENDS, GROUPS, SETTINGS. And i get a blank page to register.

    I already connect the right pages in the “Pages” tab in buddypress settings.. And still i get the message:

    Not Found
    The requested URL /members/admin/activity/ was not found on this server.
    Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

    Or when i trie it with an other account:

    Not Found

    The requested URL /members/juuderd/profile/ was not found on this server.

    Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

    I also installed a theme that is compatible with buddypress so i don’t know what i’m doing wrong anymore. Can someone please help me>

    #257597
    danbp
    Participant

    Don’t hack core files. (never !) Which language do you use ?

    Where have you done this ?
    I’ve took a look at the files and found out that the working ones are using __( "Foo %s", "buddypress" ) and the wrong ones are using _x( "Foo %s", "Blah blah blah", "buddypress" ).

    Actually all BP strings are working. If you need custom translation, you have to do that in po/mo and to use preferably the original strings. For example, if you build a template and want to use the notification count on it, you copy the original string to your work. If you don’t do that, you nedd 2 po/mo ! One for BP and one for your custom work. Which is certainly not the best and easiest solution.

    The notification count is defined in wp-content/plugins/buddypress/bp-notifications/classes/class-bp-notification-component.php

    The counter itself is inside a span and is defined separately.

    /* translators: %s: Unread notification count for the current user */
    _x( 'Notifications %s', 'Profile screen nav', 'buddypress' ),
    sprintf(
    	'<span class="%s">%s</span>',

    The bla bla bla part you mention is a comment for translators who indicates him the context. This comment appears in the pot file like: Notification %s [Profile screen nav]

    That’s BP’s default coding. You haven’t to modify this. Use it as is.

    If you have difficulty to translate such a string, you have to check your theme or any custom function you added (by hand or another plugin).

    The corresponding custom string should reflect the original one as gettext function wouldn’t translate a string which is not identic.

    __( "Foo %s", "buddypress" ) and _x( "Foo %s", "Blah blah blah", "buddypress" ) are both working, and do technicaly the same thing (foo = something) which let you modify “foo” in both case, but as the original string is _x( "Foo %s", "Blah blah blah", "buddypress" ), you couldn’t replace it with __( "Foo %s", "buddypress" ). It is the translation prefix (_x or __) which is the important thing here.

    Hope to be clear. And you’re right, unfortunately your changes will be overwriten at next BP update as you hacked core files.

    #255213
    m1000
    Participant

    After upgrade the custom loop with notifications doesn’t work anymore:

    if ( is_user_logged_in() ) {
       if ( function_exists( 'bp_is_active' ) ) {
    	if ( $notifications = bp_notifications_get_notifications_for_user( bp_loggedin_user_id(), $format = 'string' ) ) { ?>
    	<div class="notif-container">
    	<?php
    	}
    
    	if ( $notifications ) {
    		$counter = 0;
    		for ( $i = 0, $count = count( $notifications ); $i < $count; ++$i ) {
    			$alt = ( 0 == $counter % 2 ) ? ' alt' : ''; ?>
    			<div class="my-notification<?php echo $alt ?>"><?php echo $notifications[$i] ?></div>
    
    			<?php
    			 $counter++;
    			} ?>
    	</div><!-- notif-container -->
    	<?php
    	} else {}
       }
    }

    It returns ‘Array’

    berkelmudez
    Participant

    After a lot of looking it turns out basically all the SMTP plugins that I was using/testing with weren’t working. For those that might encounter the same problem, these were plugins that did not work:
    – Configure SMTP
    – Easy WP SMTP
    – WP-Mail-SMTP

    The plugin for me that did succeed in setting the smtp for buddypress email notifications was:
    – Postman SMTP

    I don’t have any clue whatsoever why those plugins don’t work with my site and why that one did. But it’s so it’s alright I guess.

    Mike
    Participant

    I got this working in the end… I had to change the first letter of ‘Primary’ menu to lowercase! Is there code I can add to not display ‘0’ after messages, for instance if there are no new messages, then ‘0’ is not displayed?

    I’ve got another plugin for Buddypress called ‘Compliments’ which also has a notification counter, is there a way to clear the reset counter of those notifications, once a person has viewed that particular page?

    Many thanks.

    @mcuk
    Participant

    Hi @mikee1001,

    The code i used for inserting the bubble counters for notifications, unread messages and total friends is:

    // Notification counter bubble
    function bptest_main_nav_notification_bubble( $items, $args ) {
        if( $args->theme_location == 'header-menu' ) { // In manage locations via WP Dash>Appearance>Menus
        	$items_array = array();
            while ( false !== ( $item_pos = strpos ( $items, '<li', 3 ) ) ) {
                $items_array[] = substr( $items, 0, $item_pos );
                $items = substr( $items, $item_pos );
            }
            $items_array[] = $items;
            array_splice( $items_array, 3, 0, '<li class="bubble">' . bp_notifications_get_unread_notification_count( bp_loggedin_user_id() ) . '</li>' ); // 0,0 1st position, 1,0 2nd, etc
            $items = implode( '', $items_array );
        }
        return $items;
    }
    add_filter( 'wp_nav_menu_items', 'bptest_main_nav_notification_bubble', 10, 2 );
    // Unread message counter bubble 
    function bptest_main_nav_message_bubble( $items, $args ) {
        if( $args->theme_location == 'header-menu' ) { // In manage locations via WP Dash>Appearance>Menus
            $items_array = array();
            while ( false !== ( $item_pos = strpos ( $items, '<li', 3 ) ) ) {
                $items_array[] = substr( $items, 0, $item_pos );
                $items = substr( $items, $item_pos );
            }
            $items_array[] = $items;
            array_splice( $items_array, 5, 0, '<li class="bubble">' . bp_get_total_unread_messages_count( bp_loggedin_user_id() ) . '</li>' ); // 0,0 1st position, 1,0 2nd, etc
            $items = implode( '', $items_array );
        }
        return $items;
    }
    add_filter( 'wp_nav_menu_items', 'bptest_main_nav_message_bubble', 10, 2 );
    // Total friends counter bubble
    // Note, 'bp_friend_get_total_requests_count' will give the total connection request count if desired
    function bptest_main_nav_friend_bubble( $items, $args ) {
        if( $args->theme_location == 'header-menu' ) { // In manage locations via WP Dash>Appearance>Menus
            $items_array = array();
            while ( false !== ( $item_pos = strpos ( $items, '<li', 3 ) ) ) {
                $items_array[] = substr( $items, 0, $item_pos );
                $items = substr( $items, $item_pos );
            }
            $items_array[] = $items;
    		array_splice( $items_array, 7, 0, '<li class="bubble">' . friends_get_total_friend_count( bp_loggedin_user_id() ) . '</li>' ); // 0,0 1st position, 1,0 2nd, etc
            $items = implode( '', $items_array );
        }
        return $items;
    }
    add_filter( 'wp_nav_menu_items', 'bptest_main_nav_friend_bubble', 10, 2 );

    In each of the above you will want to change the theme_location == 'header-menu' to theme_location == 'INSERT YOUR HEADER MENU NAME HERE'. The name of your menu is found via your WP Dashboard > Appearance > Menus. The three functions above were inserted into bp-custom.php .

    The positions of the bubbles will also need changing depending on where they should be in your menu. Do that by changing the numbers on the section array_splice( $items_array, 7, 0, '<li class="bubble">'. The comment alongside the line in the code should help you.

    You’ll also need some CSS for the bubbles otherwise you probably won’t see them even if they are functioning. If you hit ctrl-A to select everything on your web page, they should show up somewhere in your menu (assuming the code has been implemented correctly of course).

    As you can see in the functions above all the bubbles have been given a class called “bubble”. So put in your style.css (of your child theme) something like:

    #main-navigation .main-nav ul li.bubble {
    	transform: translate(0, 50%);
    	width: 20px;
    	height: 20px;	
    	padding: 2px 6px; 
    	border: 1px solid #000;
    	border-radius: 50%;
    	margin: 0 10px 0 0;
    	background: #fff;
    	color: #000;
    	font-size: 11px;	
    }

    The selector (#main-navigation .main-nav ul li.bubble) may be different on your site because it will depend on your theme etc. So use the developer tools, F12 button, on your browser to find out the correct one. It will be the bit before the .bubble that is different.

    Hopefully the above helps!

    Mike
    Participant

    Hi @mcuk , I’ve added Buddypress menu items (e.g. messages, notifications etc.) in my main WordPress menu, together with WordPress menu items (e.g. home). I also want to have the notification counters next to each of the Buddypress items, but am struggling to get your solution to work for me (doesn’t help I’m not that proficient in coding)! Should I add the code above to the functions.php of my child theme, and is it only that one file I need to edit?

    Many thanks.

    #250459
    Mustaasam Saleem
    Participant

    This question is already answered many times.
    Please have a look. πŸ™‚
    https://buddypress.org/support/search/notifications+counter/

    #247583
    shanebp
    Moderator

    Try:

    function my_counter_nav_menu($menu) {  
    
         $notif_url = bp_core_get_user_domain(bp_loggedin_user_id()) .'notifications/';
         $friends_url = bp_core_get_user_domain(bp_loggedin_user_id()) .'friends/'; 
         $msg_url = bp_core_get_user_domain(bp_loggedin_user_id()) .'messages/';    
          
        if (!is_user_logged_in())
            return $menu;
        
        $notif = '<li class="notify"><a href=" ' .$friends_url. ' ">Connections <div class="friendnumber topnavcount">'.friends_get_friend_count_for_user( bp_loggedin_user_id() ) .'</div></a></li>';
    
        if ( bp_notifications_get_unread_notification_count( bp_loggedin_user_id() ) != 0 )
            $notif .= '<li class="notify"><a href=" ' .$notif_url. ' ">Notifications <div class="notifynumber topnavcount">'. bp_notifications_get_unread_notification_count( bp_loggedin_user_id() ).'</div></a></li>';
    
       if ( bp_get_total_unread_messages_count( bp_loggedin_user_id() ) != 0 )
            $notif .= '<li class="notify"><a href=" ' .$msg_url. ' ">Messages <div class="messagenumber topnavcount">'.bp_get_total_unread_messages_count( bp_loggedin_user_id() ) .'</div></a></li>';
    
        $menu .= $notif;
        return $menu;
    }
    add_filter( 'wp_nav_menu_items', 'my_counter_nav_menu' );
    #244069
    danbp
    Participant

    hi ailyroot,

    no time to sort out what you need, but see here 3 snippets using different wya to add such item to a theme main menu or custom menu, with fixed or free position… Test and take the one you need.

    //fixed position
    function my_nav_menu_notif_counter($menu) {      
            if (!is_user_logged_in())
                    return $menu;
            else
                    $notif = '<li>Notif '. bp_notifications_get_unread_notification_count( bp_loggedin_user_id() ) .'</li>';
                    $menu = $menu . $notif;
                    return $menu;
    }
    add_filter( 'wp_nav_menu_items', 'my_nav_menu_notif_counter' );
    
    //choose position
    function my_nav_menu_positioned_notif_counter( $items, $args ) 
    {
        if( $args->theme_location == 'primary' ) // only for primary menu
        {
            $items_array = array();
            while ( false !== ( $item_pos = strpos ( $items, '<li', 3) ) )
            {
                $items_array[] = substr($items, 0, $item_pos);
                $items = substr($items, $item_pos);
            }
            $items_array[] = $items;
            array_splice($items_array, 0, 0, '<li>Notif '. bp_notifications_get_unread_notification_count( bp_loggedin_user_id() ) .'</li>'); // 0,0 is first position, 1,0 is second, etc
    
            $items = implode('', $items_array);
        }
        return $items;
    }
    add_filter('wp_nav_menu_items','my_nav_menu_positioned_notif_counter', 10, 2);
    
    // depending the theme, $theme_location may vary and this one use a menu ID
    function my_notif_link( $items, $args ) {
    
    $theme_location = 'primary';// Theme Location slug
    $existing_menu_item_db_id = 6; // menu id
    $new_menu_item_db_id = 66; // unique id number
    $label = 'Notificationas '. bp_notifications_get_unread_notification_count( bp_loggedin_user_id() );
    $url = bp_core_get_user_domain(bp_loggedin_user_id()) .'notifications/';
     
    if ( $theme_location !== $args->theme_location ) {
    	return $items;
    }
    $new_links = array();
    
    if ( is_user_logged_in() ) {
    	  
    	// only if user is logged-in, do sub-menu link
    	$item = array(
    		'title'            => $label,
    		'menu_item_parent' => $existing_menu_item_db_id,
    		'ID'               => 'fugit', // menu name
    		'db_id'            => $new_menu_item_db_id,
    		'url'              => $url,
    		'classes'          => array( 'menu-item' )
    	);
    
    	$new_links[] = (object) $item;  // Add the new menu item to our array
    	unset( $item ); // in case we add more items below
    
    	$index = count( $items );  // integer, the order number.
    
    // Insert the new links at the appropriate place.
    	array_splice( $items, $index, 0, $new_links ); // 0,0 is first position, 1,0 is second, etc
    
    }
    
    return $items;
    }
    add_filter( 'wp_nav_menu_objects', 'my_notif_link', 10, 2 );

    Happy testing πŸ˜‰

    #243049
    danbp
    Participant

    Yes you can, but why would you do that as all those counters are already in the user menu (on top right, under Howdy) and on each profile ? Tsssss…., but you’re the boss ! πŸ˜‰

    To count friends use friends_get_friend_count_for_user( bp_loggedin_user_id() );
    To count messages use bp_get_total_unread_messages_count( bp_loggedin_user_id() )

    Complete solution

    function my_counter_nav_menu($menu) {  
    
    $notif_url = bp_core_get_user_domain(bp_loggedin_user_id()) .'notifications/';
    $friends_url = bp_core_get_user_domain(bp_loggedin_user_id()) .'friends/'; 
    $msg_url = bp_core_get_user_domain(bp_loggedin_user_id()) .'messages/';    
          
        if (!is_user_logged_in())
            return $menu;
        else
            $notif = '
    
    <li><a href=" ' .$notif_url. ' ">Notif ['. bp_notifications_get_unread_notification_count( bp_loggedin_user_id() ) .']</a></li>
    <li><a href=" ' .$friends_url. ' ">Friends ['. friends_get_friend_count_for_user( bp_loggedin_user_id() ) .']</a></li> 
    <li><a href=" ' .$msg_url. ' ">Messages ['.  bp_get_total_unread_messages_count( bp_loggedin_user_id() ) .']</a></li>
    
    ';
    				
        $menu = $menu . $notif;
        return $menu;
    }
    add_filter( 'wp_nav_menu_items', 'my_counter_nav_menu' );

    Bu yardΔ±m olabilir ! πŸ˜‰

    #243047
    danbp
    Participant

    Add this to bp-custom.php and give a try ! Should show at the left of the logout button on Buddy theme nav bar

    function my_nav_menu_notif_counter($menu) {  
    
    $url = bp_core_get_user_domain(bp_loggedin_user_id()) .'notifications/';   
           // condition: user must be loggedin
            if (!is_user_logged_in())
                    return $menu;
            else
                    $notif = '<li><a href=" ' .$url. ' ">Notif ['. bp_notifications_get_unread_notification_count( bp_loggedin_user_id() ) .']</a></li>';
    				
                    $menu = $menu . $notif;
                    return $menu;
    }
    add_filter( 'wp_nav_menu_items', 'my_nav_menu_notif_counter' );
    Osisis
    Participant

    Have you sorted this out yet? If not, if your nav has drop down capabilities you could add the menu items in the same order they are in the adminbar. For notifications, check out this post here.

    @mcuk
    Participant

    Thanks @danbp, exactly what i needed to position the counter bubbles! Both snippets worked great.
    I updated the theme_location arg in the second to get it to work with my menu and added an ID to allow the CSS. Thanks again!

    For anyone looking to do the same with the messages and friends request counters, replace the:

    bp_notifications_get_unread_notification_count

    with

    bp_get_total_unread_messages_count or bp_friend_get_total_requests_count as needed.

    danbp
    Participant

    Two snippets to do this (hopefully). The first has a fixed position (latest by default), the second let’s you choose the position.

    function my_nav_menu_notif_counter($menu) {      
            if (!is_user_logged_in())
                    return $menu;
            else
                    $notif = '<li>Notif '. bp_notifications_get_unread_notification_count( bp_loggedin_user_id() ) .'</li>';
                    $menu = $menu . $notif;
                    return $menu;
    }
    add_filter( 'wp_nav_menu_items', 'my_nav_menu_notif_counter' );
    
    See comment how to apply a position
    function my_nav_menu_positioned_notif_counter( $items, $args ) 
    {
        if( $args->theme_location == 'primary' ) // only for primary menu
        {
            $items_array = array();
            while ( false !== ( $item_pos = strpos ( $items, '<li', 3) ) )
            {
                $items_array[] = substr($items, 0, $item_pos);
                $items = substr($items, $item_pos);
            }
            $items_array[] = $items;
            array_splice($items_array, 0, 0, '<li>Notif '. bp_notifications_get_unread_notification_count( bp_loggedin_user_id() ) .'</li>'); // 0,0 is first position, 1,0 is second, etc
    
            $items = implode('', $items_array);
        }
        return $items;
    }
    add_filter('wp_nav_menu_items','my_nav_menu_positioned_notif_counter', 10, 2);

    Reference: https://developer.wordpress.org/reference/hooks/wp_nav_menu_items/

Viewing 25 results - 26 through 50 (of 75 total)
Skip to toolbar