Fun Little Task Involving the Ultimate Member Plugin


I was recently asked if I could help make the “User’s Activity Wall” of the Ultimate Social Activity extension for the Ultimate Member plugin include all friend posts and all Administrator posts.  Initially, I thought that surely that functionality exists, but low and behold, I couldn’t find it.   It seemed as if the Administrators of the site could approve, edit, and delete posts, but if they wanted to make a global announcement, they couldn’t.  It is possible that I missed it and if I did, oh well, but as it turned out it was quite a fun little project.

First, I got a site spun up with the required plugins and then I created a handful of users.  Some were administrators and others were just contributors.  I then connected a few of the accounts with other accounts to create the “Friend” relationships.  I also started making posts and comments logged in as the Admin and as the Contributors.

If a user (contributor) was friends with an admin, that user saw all friend comments as well as admin posts.  However, if a user hadn’t friended any admin, they only saw their posts/comments and posts/comments of those they have “friended”

So, this is where began digging into the plugin to see where the relationships were made and where the Query Arguments were constructed.  As it turned out, the file I needed was user-wall.php in the UM Social Activity extension.

By setting up the site locally and hooking it up to Xdebug in my VS Code, I was able to break point several points in the file and see where it checked the user’s id, as well as, all friend ids and include them in the

$wallposts = new WP_Query( $args );

Next was figuring out the $args and somehow making sure that I could also add Admin users to the $args

First, I had to do a “new WP_User_Query” for all roles that were equal to ‘administrator’ – As it turned out, I didn’t even need the foreach in the snippet below, but I left it and then commented out echo to show how I was testing along the way and seeing how the admin ids were getting returned.

$administrator_ids =  new WP_User_Query( array( 'role' => 'administrator' ) );
                $admins = $administrator_ids->get_results();
                $admin_ids = array();
                foreach($admins as $admin) {
                    $admin_ids[] = $admin->ID;
                }
                $args2 = implode(',', $admin_ids);
                // echo $args2

Next was figuring out how to add the Admin Ids to the $args.  It wound up being as simple as doing an array_merge with the friend ids.  In the snippet below, the first line grabs the friend ids and stores them in an array called $friends_ids.  So, I used array_merge and created a new variable that stored the two arrays as a single array.

$friends_ids = UM()->Activity_API()->api()->friends_ids();
            $friends_admin = array_merge($friends_ids, $admin_ids);
            if ( $friends_ids ) {
                $args['meta_query'][] = array(
                    'key'       => '_user_id',
                    'value'     => $friends_admin,
                    'compare'   => 'IN'
                );
            }

It was downhill from there.  I was then able to update the snippet where $args gets built to include my new variable and that was it.  The query now found all friends and admin posts.

I wrapped it up in a child theme, sent it back over to my colleague and that was it.  Learning the plugin actually took longer than the solution, but that is how it goes sometimes.