I recently received a request to do something interesting with WordPress users. In addition to editing some of the capabilities of the different roles (which can be done with one of a handful of plugins), this is what needed to be done:

The ability to assign users (contributors/editors/administrators) to a specific group as members

Each group would be able to have a user assigned as leader (either an administrator or an editor)

Whenever a member (contributor) publishes a blog, the group leader is sent an email with a link to the published article

A group leader should be able to temporarily reassign his or her group to another user

Take any one of these requests by themselves and it doesn’t seem overly complicated. However, getting all these to play nicely is a bit more challenging. After spending hours scouring the internet and trying a bunch of different plugins, nothing quite covered all four of these areas.

I decided that I would take a stab at coming up with my own solution. I use ACF regularly in the websites I build, and I figured that their must be a way I could use it to accomplish this. So, lucky you, I’m going to walk through what I came up with.

Setting Up a Custom Post for the User Groups

First thing that needs to happen is setting up a custom post type for the user groups. If you don’t know how to setup a custom post type, go and read my post about it. The important thing to remember here is what you set as the slug for the custom post type. It will be used later on in the code.

Adding Custom Fields to the New Custom Post Type

Now that the custom post type is setup, custom fields can be added to it. For this article, I’m going to go ahead and assume that you are familiar with ACF and how to setup custom fields. For the user group, we are going to need:

A user field to select a group leader

to select a group leader A true/false field to temporarily change the group leader if needed

Another user field to select that temporary leader. This can be set to conditionally show when the above field is checked. That’s not required, but it’s nice to have.

to select that temporary leader. This can be set to conditionally show when the above field is checked. That’s not required, but it’s nice to have. A repeater field for members. This repeater field will only have one sub field which will be yet another user field to add members to this group.

Each of the above user fields can be limited to specific roles if that is a need. In this example, group leaders, both regular and temporary, can only be administrators or editors. Anyone can be a member of the group, but that can also be limited if need be (to just contributors for example).

The only thing left to do is to assign this custom field group to the new “Groups” custom post type that was set up earlier. The setup for everything should look something like this:

It is important to note what the “names” are for each of your custom fields. They will all be used later for the email notifications. With the custom fields created, it is now possible to go into the “Groups” custom post type and add a group. A group leader can be assigned and members can be added. The group would look something like this:

Setting Up Email Notifications

The only thing left to do is to setup the email notifications. The following code can be placed in the functions.php file:

add_action('publish_post', 'notifyauthor'); function notifyauthor() { $loop = new WP_Query(array('post_type' => 'groups', 'posts_per_page' => -1, 'order' => 'ASC')); while ($loop->have_posts()) : $loop->the_post(); if(have_rows('members')): while (have_rows('members')) : the_row(); $author = get_sub_field('member'); $author_id = $author['ID']; $leader = get_field('group_leader'); $leader_id = $leader['ID']; $temp = get_field('temp_group_leader'); $temp_id = $temp['ID']; $current = get_current_user_id(); if($author_id == $current) { $subject = 'Post Published: ' . $post->post_title . ''; if(get_field('temp_group_leader') && get_field('temp_leader')) { $message = 'Hi ' . get_the_author_meta('first_name', $temp_id) . ', Looks like ' . get_the_author_meta('first_name', $author_id) . ' ' . $author_last = get_the_author_meta('last_name', $author_id) . ' just published a new post: ' . get_permalink($post_id) . '.'; wp_mail(get_the_author_meta('user_email', $temp_id), $subject, $message); } else { $message = 'Hi ' . get_the_author_meta('first_name', $leader_id) . ', Looks like ' . $author_name . ' ' . $author_last . ' just published a new post: ' . get_permalink($post_id) . '.'; wp_mail(get_the_author_meta('user_email', $leader_id), $subject, $message); } } endwhile; endif; endwhile; wp_reset_query(); }

There’s a bunch of different things going on here. Let’s go over each piece one by one so that it’s easier to understand what’s going one here:

1. Lines 1-2: Create a function and add an action which determines when that function will run. In this case, the function is named notifyauthor, but this can be anything, but the name needs to be consistent.

2. Lines 4-5: Loop through the “Groups” custom post type. This just grabs all of the different groups that have been created. Just be sure that the slug you set for the custom post type is what is used for ‘post_type’ => ‘groups’

3. Lines 7-8: Check to make sure that the group has members. This is where the name for repeater field comes in. In the example above, the name was set to members. Again, that can be whatever is needed, but needs to remain consistent.

4. Lines 10-15: If the group has members, create variables from the users data. Here, data is being pulled for a few different users:

First, the data of the member.

Second, the data of the group leader.

Third, data from the temporary group leader if there is one.

Fourth, the user ID of the current user (the person who is publishing the post).

The IDs of all the users are being grabbed as well. This is so we can get data like name and email address.

5. Lines 17-27: Use the above variables for some if/then statements:

First, check to see if the current user (person publishing the post) belongs to a group. If so, set a subject for the email.

Second, check to see if a temporary group leader has been set. If so, set the message to send to the temporary group leader.

Third, if there is no temporary group leader, set the message to send to the group leader.

That’s it! Now whenever a member of a group publishes a new post, a notification will be sent to their group leader (or temporary one). I’m not sure how ubiquitous this solution will be, but it was fun to figure out anyway.

Questions or concerns? Is this helpful? Let me know by commenting below.