Skip to:
Content
Pages
Categories
Search
Top
Bottom

How To: notification for member leaving group with show filter


  • aljo1985
    Participant

    @aljo1985

    This outputs “test left the group Group logo of Test GroupTest Group 16 seconds ago”
    Into your activity feed. Useful if you want to track who has currently left your group. As there is no way of knowing if your groups get large. It also inserts in show with disbanded groups to view all members who have disbanded.
    Just insert this code into your themes functions.php file and you are good to go.
    Preview Left Group Preview

    //////////////////////////////////////////
    ////////// LEFT GROUP ////////////////////
    //////////////////////////////////////////
    add_action( 'groups_leave_group', 'groups_left_group', 10, 2 );
    function groups_left_group( $group_id, $user_id = 0 ) {
    	global $bp;
    
    	if ( empty( $user_id ) )
    		$user_id = bp_loggedin_user_id();
    
    	// Record this in activity streams
    	groups_record_activity( array(
    		'type'    => 'left_group',
    		'item_id' => $group_id,
    		'user_id' => $user_id,
    	) );
    
    	// Modify group meta
    	groups_update_groupmeta( $group_id, 'last_activity', bp_core_current_time() );
    
    	return true;
    }
    function groups_register_left_actions() {
    	$bp = buddypress();
    
    	if ( ! bp_is_active( 'activity' ) ) {
    		return false;
    	}
    
    	bp_activity_set_action(
    		$bp->groups->id,
    		'left_group',
    		__( 'Left group', 'buddypress' ),
    		'bp_groups_format_activity_action_left_group',
    		__( 'Group Disbands', 'buddypress' ),
    		array( 'activity', 'group', 'member', 'member_groups' )
    	);
    
    	do_action( 'groups_register_activity_actions' );
    }
    add_action( 'bp_register_activity_actions', 'groups_register_left_actions' );
    function bp_groups_format_activity_action_left_group( $action, $activity ) {
    	$user_link = bp_core_get_userlink( $activity->user_id );
    
    	$group = groups_get_group( array(
    		'group_id'        => $activity->item_id,
    		'populate_extras' => false,
    	) );
    	$group_link = '<a href="' . esc_url( bp_get_group_permalink( $group ) ) . '">' . esc_html( $group->name ) . '</a>';
    
    	$action = sprintf( __( '%1$s left the group %2$s', 'buddypress' ), $user_link, $group_link );
    
    	return apply_filters( 'bp_groups_format_activity_action_joined_group', $action, $activity );
    }

    I do have a question for the developers here, how do you make this so that the join/left activity log cannot be deleted by the user who joined/left? I want it so that if a person joins/leaves, then they can’t delete their join/left activity from the log.

    I could probably use
    groups_is_user_member( $user_id, $group_id )
    For when the member leaves a group but what about posting that they joined a group? I would prefer if admins/mods of the group could only delete those.

    Enjoy, and hope someone can answer this question also.

  • The topic ‘How To: notification for member leaving group with show filter’ is closed to new replies.
Skip to toolbar