Automatically Create Tags for Users in WordPress Roles

We had an interesting question from a PublishPress user this week:

Is there any way to automatically add a specific tag if the user is in a certain role?

For example, Authors will always get “Tag A” added and Editors will always get “Tag B” added. Yes, this is possible with a little code.

Once this is done, you can use the Tags to organize the content or to trigger other functionality. For example, you can use PublishPress notifications to send an email for any post that has the Tag you choose.


How to Create Tags for User Roles

Add this code to your theme's functions.php file. It will automatically add Tags for any users in the Administrator and Author roles.

add_action('save_post', 'setPostTagBasedOnUserRole', 200, 2);
function setPostTagBasedOnUserRole($postId, $post)
{
    $allowedPostTypes = [
        'post',
    ];

    $userRoleTagMap = [
        'administrator' => 'tag1',
        'author'        => 'tag2',
    ];


    if (!in_array($post->post_type, $allowedPostTypes)) {
        return;
    }

    $user = get_user_by('id', $post->post_author);

    if (empty($user) || is_wp_error($user)) {
        return;
    }

    foreach ($userRoleTagMap as $role => $tag) {
        if (in_array($role, $user->roles)) {
            wp_set_post_tags($postId, $tag, true);
        }
    }
}

There are two items you may need to change in this code:

  • $allowedPostTypes: Enter any post types you want to add the Tags for.
  • $userRoleTagMap: Enter the names of your roles and your Tags.

After adding this code to your site, every user in the “Author” role will automatically have “tag2” added to their new posts.

Tag Added Author
Tag Added Author

Every user in the “Administrator” role will automatically have “tag1” added to their new posts.

Tag Added Admin
Tag Added Admin

It's also possible to adapt this code for other situations. For example, if you're using the Groups feature in PublishPress Permissions, you can create Tags for specific groups using this code:

function setPostTagBasedOnUserRole($postId, $post) 
{ 
	$allowedPostTypes = [ 
		'post', 
	]; 

	// set these to your actual group IDs
	$groupA = 12;
	$groupB = 13;

	$userGroupTagMap = [          
		$groupA => 'tag1', 
		$groupB => 'tag2', 
	]; 

	if (!in_array($post->post_type, $allowedPostTypes)) {         
		return;      
	} 

	if (!function_exists('presspermit')) {
		return;
	}

	if(!$authorGroups = presspermit()->groups()->getGroupsForUser($post->post_author)) {
		return;
	}

	foreach ($userGroupTagMap as $groupID => $tag) { 
		if (!empty($authorGroups[$groupID])) { 
			wp_set_post_tags($postId, $tag, true); 
		} 
	} 
}

More Tags Tutorials

We have another, similar tutorial available showing how to use author data to automatically create WordPress tags. That allows you to pull any information from your Author profiles and use that to create Tags.

PublishPress plugins also allow you to do a variety of useful tricks with no code. For example, you can stop users from creating new Tags, or block Users from adding a specific category or tag. You can even specify a minimum or maximum number of Tags per post.

If you want to manage your WordPress taxonomies, check out the TaxoPress plugin.

Leave a Reply

Your email address will not be published. Required fields are marked *