Skip to:
Content
Pages
Categories
Search
Top
Bottom

[Resolved] How To Get Notification Count? (Code)?


  • chatty24
    Participant

    @chatty24

    Hey,

    I am trying to change the page title whenever there are notifications.

    If a user has more than 0 notifications then it should show, ‘You Have New Notifications’.

    I have tried out this code :

    $notifications = bp_notifications_get_notifications_for_user( bp_loggedin_user_id(), 'object' );
        $count         = ! empty( $notifications ) ? count( $notifications ) : 0;
    
    if ( $count>0 ) {
      add_filter('wp_title','my_page_title',10,1);
    
    function my_page_title($title){
        $title='You Have New Notifications'; 
        return $title;
    }
      }

    But, it is not working. It is only working when I change the value of this code

    $count = ! empty( $notifications ) ? count( $notifications ) : 0;

    from 0 to 1,2 etc. Something like this,

    $count = ! empty( $notifications ) ? count( $notifications ) : 2;

    So, it is reading this end number as the number of notifications. I referred the notification code from here

    How can I make this code work?

    Thanks
    Have a nice day!

Viewing 25 replies - 1 through 25 (of 28 total)

  • danbp
    Participant

    @danbp

    Try this

    if ( $notifications = bp_notifications_get_notifications_for_user( $user_id, $format = 'string' ) ) { 
    
    		$counter = 0;
    			for ( $i = 0; $i < count($notifications); $i++ ) {
    			$alt = ( 0 == $counter % 2 ) ? ' class="alt"' : ''; 
    
    			echo '<span ="'.$alt.'">'. $notifications[$i] .'</span>';
    
    			$counter++;
    			}	

    chatty24
    Participant

    @chatty24

    @danbp

    Thanks for the answer but, what is the notification count in this code? I mean, what would be the term that I would use to compare with ‘0’.

    if ( what_do_I_keep_here >0) {
    
    // Some action here
    
    }

    Thanks


    chatty24
    Participant

    @chatty24

    @danbp

    count($notifications) is returning the value to ‘1’ no matter whatever the notification count is…


    Henry Wright
    Moderator

    @henrywright

    There’s 2 types of notifications: a) read and b) unread.

    bp_notifications_get_unread_notification_count( $user_id ) will get you the unread count so you could use that value in the comparison you mentioned here.


    chatty24
    Participant

    @chatty24

    @henrywright

    Thanks for the reply but

    I used the below code to print the notification count on my page to match it with the notification count which is shown by wordpress and I noticed that the below code returns the value to zero no matter what ever the original notification count is.

    
    echo bp_notifications_get_unread_notification_count( $user_id );

    and yes I need unread notification count.


    Henry Wright
    Moderator

    @henrywright

    How are you getting the user’s ID $user_id?


    chatty24
    Participant

    @chatty24

    @henrywright

    Opps… I did not call it.

    But, how do I get it?


    Henry Wright
    Moderator

    @henrywright

    …how do I get it?

    That depends. For example:

    If you want the currently logged-in user:

    $user_id = bp_loggedin_user_id();

    If the context is a member’s profile page and you want the displayed member:

    $user_id = bp_displayed_user_id();

    So, depending on the above, what you need may be either:

    bp_notifications_get_unread_notification_count( bp_loggedin_user_id() );

    or

    bp_notifications_get_unread_notification_count( bp_displayed_user_id() );

    Because we’re talking about notifications, it’s likely you’ll want the former because you wouldn’t really need to show somebody else’s notifications count.


    chatty24
    Participant

    @chatty24

    @henrywright

    I tried using the below code but it is returning the value to zero too 🙁

    echo bp_notifications_get_unread_notification_count( bp_loggedin_user_id() );


    Henry Wright
    Moderator

    @henrywright

    Then perhaps you have zero unread notifications?


    chatty24
    Participant

    @chatty24

    @henrywright
    No.. You can look at the screenshot below


    danbp
    Participant

    @danbp

    Try this (tested on 2.2 RC2)

    function bpfr_add_notification_to_page_title( $title, $original_title, $sep, $seplocation  ) {
    	global $bp;
    	if( bp_is_user() && ! bp_get_member_user_id() ) {
            $user_id = 'displayed: '. bp_displayed_user_id();
        } else {
            $user_id = 'get_member_user: '. bp_get_member_user_id();
        }
    
    	if (bp_notifications_get_unread_notification_count( $user_id ) ) {
    
    		$counter = 0;
    			for ( $i = 0; $i < count($notifications); $i++ ) {
    				echo $notifications[$i];
    			$counter++;
    			}	
    
    	if ( bp_is_profile_component() || bp_is_current_action( 'just-me' ) ) {
    		
    			$title .= bp_notifications_get_unread_notification_count() . " $sep ";
        }
    		return $title;
    	}
    }
    add_filter( 'bp_modify_page_title', 'bpfr_add_notification_to_page_title', 9, 4 );

    chatty24
    Participant

    @chatty24

    @danbp

    The code works but only on profile pages. Can this come on each page including the home page…

    Thanks a lot for your help! 😀


    chatty24
    Participant

    @chatty24

    @danbp

    And how to hide the notification count from title bar when it is 0.

    Thanks


    chatty24
    Participant

    @chatty24

    @danbp
    I have edited the code a bit and now it is getting displayed on all the pages as I wanted but, How can I hide the modified title when the notification count is 0. By current code is below –

    function bpfr_add_notification_to_page_title( $title, $original_title, $sep, $seplocation  ) {
    	global $bp;
    	if( bp_is_user() && ! bp_get_member_user_id() ) {
            $user_id = 'displayed: '. bp_displayed_user_id();
        } else {
            $user_id = 'get_member_user: '. bp_get_member_user_id();
        }
    
    	if (bp_notifications_get_unread_notification_count( $user_id ) ) {
    
    		$counter = 0;
    			for ( $i = 0; $i < count($notifications); $i++ ) {
    				echo $notifications[$i];
    			$counter++;
    			}	
    
    	
    		
    			$title = "You Have " . bp_notifications_get_unread_notification_count() . " New Notification(s) - ";
        
    		return $title;
    	}
    }
    add_filter( 'wp_title', 'bpfr_add_notification_to_page_title', 9, 4 );
    

    Thanks


    chatty24
    Participant

    @chatty24

    And also it is showing notification of the users of whose profiles I visit. I just want the notification of the currently logged in user…

    Thanks


    Brajesh Singh
    Participant

    @sbrajesh

    Hi Chaitanya,
    Try this code.

    
    
    function bpfr_add_notification_to_page_title( $title, $original_title, $sep  ) {
    	
    	//do not change if the user is not logged in
    	if( ! is_user_logged_in() )
    		return $title;
    	
    	$user_id = get_current_user_id();//logged in user's id
    	
    	$count = bp_notifications_get_unread_notification_count( $user_id );
    	
    	if( $count > 0 )
    		$title = sprintf( "You Have %d New Notification(s) - ", $count );
    	
    	return $title;
    	
    	
    }
    add_filter( 'wp_title', 'bpfr_add_notification_to_page_title', 100, 3 );
    

    I have modified the code from your last post. The difference is, It only does it for logged in users. Also, I have decreased the priority to make it work on BuddyPress pages( otherwide bp_modify_title would have overwritten it).

    Hope that helps.


    chatty24
    Participant

    @chatty24

    @sbrajesh

    WOW!!!

    Thanks a lot Brajesh!
    You solved my problem and this is exactly what I wanted.

    Thanks a lot. I really appreciate your help.
    Have a nice day!


    Brajesh Singh
    Participant

    @sbrajesh

    You are most welcome 🙂
    Have a great day!


    danbp
    Participant

    @danbp

    @chatty24, all readers,

    the modified code used here throws a warning with BP 2.2. Don’t use it !

    FYI the title code after applying the snippet looks like this:
    <title>You Have 1 New Notification(s) - </title>
    You can probably remove the –

    Thxs @sbrajesh, you where faster as me ! 😉


    Henry Wright
    Moderator

    @henrywright

    Thanks @danbp for the update, thanks to @sbrajesh for saving the day and glad @chatty24 found a solution! 🙂


    chatty24
    Participant

    @chatty24

    @sbrajesh

    Could that also happen in real time (without refreshing the page)?

    Thanks


    AilyRoot
    Participant

    @ailyroot

    Hi guys
    we are looking for solution to get buddypress notifications work on our theme, we know it will be shown on wordpress’s default top tool bar but we want it to show somewhere else.

    We are using WP 4.3 with buddypress 2.3.3, we have added these to theme’s functions.php

    
    function bpfr_add_notification_to_page_title( $title, $original_title, $sep  ) {
    	
    	//do not change if the user is not logged in
    	if( ! is_user_logged_in() )
    		return $title;
    	
    	$user_id = get_current_user_id();//logged in user's id
    	
    	$count = bp_notifications_get_unread_notification_count( $user_id );
    	
    	if( $count > 0 )
    		$title = sprintf( "You Have %d New Notification(s) - ", $count );
    	
    	return $title;
    	
    	
    }
    add_filter( 'wp_title', 'bpfr_add_notification_to_page_title', 100, 3 );
    

    then we add these to theme ‘s menu location

    
    <?php echo bp_notifications_get_unread_notification_count( bp_loggedin_user_id() ); ?>
    

    but it is showing nothing, what is the correct steps to make this work please?

    thanks


    danbp
    Participant

    @danbp

    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 😉


    AilyRoot
    Participant

    @ailyroot

    many thanks for this!

    We now want to remove WP top toolbar (not the question here), we then want to ask how to move this whole part to a bit under, so information could join together

    is this possible?

    that area has “notifications”+ “member name”+ “search” + “drop out menu

Viewing 25 replies - 1 through 25 (of 28 total)
  • The topic ‘[Resolved] How To Get Notification Count? (Code)?’ is closed to new replies.
Skip to toolbar