Skip to:
Content
Pages
Categories
Search
Top
Bottom

Function to check if group has notifications


  • ouegy
    Participant

    @ouegy

    WordPress 4.0, BuddyPress 2.1.1

    Hey,

    I’d like to display an icon in my groups sidebar if there are new notifications related to that group.

    For messages I’m using the function:

    bp_total_unread_messages_count()

    Is there a similar function for group notifications?

    This is my code to handle displaying the groups:

    <?php if ( bp_has_groups( bp_ajax_querystring( 'groups' ) . 'max=4&type=random' ) ) : ?>
          <?php while ( bp_groups() ) : bp_the_group(); ?>
            <li class="item-title">
                <div class="grid-33">
                    <?php bp_group_avatar('type=thumb&width=40&height=40'); ?>
                </div>
                <div class="grid-66">
                    <a>"><?php bp_group_name(); ?></a><br />
                    <span><?php group_member_count() ?><span>
                    <i class="fa fa-group"></i>
                </div>
            </li>
          <?php endwhile; ?>
        <?php endif; ?>

    Thanks in advance!

Viewing 3 replies - 1 through 3 (of 3 total)

  • danbp
    Moderator

    @danbp

    hi ouegy,

    Add this snippet to your child-theme functions.php and give it a try.

    First function grabs the notification
    Second function allows to add a hook to the template (in your case sidebar.php)
    And the hook to use.

    function bpfr_sidebar_notifications_menu() {
    global $bp;
    
        // if user is not logged, we do nothing
    	if ( !is_user_logged_in() )
    	return false;
    
    	// if user is group member, show him notification + count
    	if ( groups_is_user_member( bp_loggedin_user_id(), $bp->groups->current_group->id ) && $notifications = bp_core_get_notifications_for_user( $bp->loggedin_user->id ) ) { 
    
    		echo '<ul>';
    
    		$counter = 0;
    			for ( $i = 0; $i < count($notifications); $i++ ) {
    			$alt = ( 0 == $counter % 2 ) ? ' class="alt"' : ''; 
    
    			echo '<li ="'.$alt.'"><i class="fa fa-group"></i>'. $notifications[$i] .'</li>';
    
    			$counter++;
    			echo '</ul>';
    			}	
    	}
    }
    
    function bpfr_xtra_group_sidebar() {	
    	// show notification only on the group sidebar
    	if(bp_is_groups_component() ):
    	// your content
    		 bpfr_sidebar_notifications_menu();
    	endif;
    
    }
    add_action( 'xtragroup', 'bpfr_xtra_group_sidebar' );

    The hook to add to sidebar.php (or any other template)
    <?php do_action( 'xtragroup' ); ?>

    Hope this help ! 😉


    shanebp
    Moderator

    @shanebp

    Nice @danbp, but a couple of issues…

    bp_core_get_notifications_for_user is deprecated.

    Instead use bp_notifications_get_notifications_for_user( $user_id, $format = 'string' )

    And I’d try using $format = 'object' so you can loop thru the results and only display those items related to that group.

    Of course, this will only get notifications for a single person.
    I’m not sure if there would be notifications for a group other than activity update…?


    danbp
    Moderator

    @danbp

    Thxs @shanebp, i ignored that. 🙄

    Corrected function here:

    function bpfr_sidebar_notifications_menu() {
    global $bp;
    
        // if user is not logged, we do nothing
    	if ( !is_user_logged_in() )
    	return false;
    
    	// if user is group member, show him notification + count	
    if ( groups_is_user_member( bp_loggedin_user_id(), $bp->groups->current_group->id ) && $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 '<p ="'.$alt.'"><i class="fa fa-group"></i>'. $notifications[$i] .'</p>';
    
    			$counter++;
    			}	
    	}
    }

    Shows anything group related to the concerned group member.
    Mentions, membership request/reject, promotion,…

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Function to check if group has notifications’ is closed to new replies.
Skip to toolbar