Working My Sites Filter

So previously I was struggling with stripping out the Participants blogs that the bbPress/BuddyPress combo was adding to the My Sites list. I got it working and added a bit to deal with it kicking out blogs a little too aggressively.

First to get the actual name of the Participant slug, I made a page template for a theme that only did one thing- listed the user roles array. It is dead simple and looks like so . . .

<?php
/**
 * Template Name: user roles 
 *
 * @package WordPress
 * @subpackage Lies
 * @since Twenty Fourteen 1.0
 */
     global $wp_roles;
     $roles = $wp_roles->get_names();

     // Below code will print the all list of roles.
     print_r($roles);        
?>

Throwing that in a theme let me know that the slug for participant was actually bbp_participant. I spent a lot of time thinking I was doing something else wrong. I’m also not a huge fan of doing things this way in PHP. I wish I had the a console log option like in javascript.

<?php
/*
Plugin Name: ALT Lab Remove Participant Blogs
Plugin URI: https://github.com/woodwardtw/alt-lab-participant-remover
Description: Removes all the blogs where you are just a participant
Author: Tom Woodward
Version: 1.1
Author URI: http://bionicteaching.com/
*/

function remove_non_admin_blogs($blogs) {
 				global $current_user; 
    			$user_id = $current_user->ID; 
    			$role = 'bbp_participant';

                foreach ( $blogs as $blog_id => $blog ) {

                    // Get the user object for the user for this blog.
                    $user = new WP_User( $user_id, '', $blog_id );
                    // if they only have one user role for that blog . . . then 
				if (count( $user->roles ) === 1 ){
                    // Remove this blog from the list if the user is just a participant
                    if (in_array( $role, $user->roles ) ) {
                        unset( $blogs[ $blog_id ] );
                    }
                }
            }

                return $blogs;
            }    
add_filter( 'get_blogs_of_user', 'remove_non_admin_blogs' );
?>

2 thoughts on “Working My Sites Filter

Comments are closed.