Filtering the My Sites List (Again)

I made a filter a while back to keep people who were associated with blogs through the bbpress/buddypress auto-enrollment feature from having tons of sites show up in their My Sites list. It was never a perfect solution and felt sloppy because it relied on having only one role and that role being bbp_participant. It also failed when I un-network activated bbPress because it couldn’t filter people who had no role (and there was still some invisible connection). And the final nail was that it seemed to fail intermittently and I had no idea why.

Here is a very different path to the same result but one that appears to work better and will be proof against the ‘no role’ issue that hampered me earlier. It’s all made possible by the delightful array_interesect function. It’s one of those things that I am confident I’ll use down the road. I have no idea if it’s as commonly known as ABC or whatever but I had no idea it existed.

This actually came to fruition on a plane trip because Mark Luetke told me about Varying Vagrant Vagrants and I was able to spend about twenty minutes working on this even without Internet. It has also made me reflect that lack of interruptions really improves my productivity on things like this.

<?php
/*
Plugin Name: ALT Lab Remove Ugly 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.5
Author URI: http://bionicteaching.com/
*/
function remove_junk_blogs($blogs) {
 				global $current_user; 
    			$user_id = $current_user->ID; 
                $allowed_roles = array( 'administrator', 'editor', 'author', 'contributor','subscriber'); //you can remove some of these and reduce the sites that would show
                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 (array_intersect($allowed_roles, $user->roles) ) {
                    }
                    else
                      unset($blogs[$blog_id]);
            }
                return $blogs;
            }    
add_filter( 'get_blogs_of_user', 'remove_junk_blogs' );
?>

Comments on this post

  1. jugoretz said on March 10, 2016 at 2:59 pm

    Oh wow, REALLY? This is so brilliant! Solves a problem I’ve been trying to figure out for years! Thanks, Tom!

    • Tom Woodward said on March 10, 2016 at 3:27 pm

      It’s been killing me and seems to work well here. I’d be interested in knowing if it works for you.

      • jugoretz said on March 11, 2016 at 1:27 pm

        Seems like it does work–for most cases (I’ve had some other issues with other plugins that automatically add visitors as other roles–author, for example. So still working on those. Thanks again!

Leave a Reply

Trackbacks and Pingbacks on this post

No trackbacks.

TrackBack URL