Use Author Data to Automatically Create WordPress Tags

This week, one PublishPress customer was in a situation where they needed to create Tags based on the Authors of a post.

The goal was that when someone writes a post, their name was automatically added as a Tag.

Once this was done, the customer could use the Tags to trigger other functionality. For example, they could use PublishPress notifications to send an email for any post that has the Tag for specific authors.

We wrote some code that automatically adds Author details as Tags. You can pull any information from your Author profiles and use that to create Tags.


How to Create Tags for Authors

Add this code to your theme's functions.php file:

add_action('save_post', 'add_authors_name', 200, 2);
function add_authors_name($post_id, $post)
{
    if ($post->post_type !== 'post' || function_exists('get_multiple_authors')) {
        return;
    }
    $user = get_user_by('id', $post->post_author);
    wp_set_post_tags($post_id, $user->display_name, true);
}

This code is written for Posts, but can be adapted for any post type. Just note that many custom post types don't have support for authors. Here's how to add authors to any custom post type.

Once this code is live, go to write a new post. Whatever name is in the “Author” field will be instantly added as a Tag.

Add Author Tags
Add Author Tags

Some things to note about this code:

  1. The new Tag will be visible immediately, as soon as you open the post for editing.
  2. It is retroactive: this code will also add Tags for existing posts on your site.
  3. We added support for our PublishPress Authors plugin using this code function_exists('get_multiple_authors')), but you can safely remove that.
  4. We are using the author's “Display Name” but you can replace that with any of these alternatives. For example, replace $user->display_name with $user_email->display_name and the authors' email address will be used to create the Tags.
  5. This code supports Posts, but can be modified. There are a number of plugins and code snippets that allow you to add Tags to other post types. For example, you want to add support for Pages, you use the the “Tag Pages” plugin and update the post type here:
if ($post->post_type !== 'page'

How to Create Tags from PublishPress Authors

This code can be expanded if you're using the PublishPress Authors plugin. Add this code to your functions.php file, below the code snippet we posted earlier:

// Only needed if PublishPress Authors is activated.
add_action('set_object_terms', 'add_multiple_authors_name', 200, 4);
function add_multiple_authors_name($object_id, $terms, $tt_ids, $taxonomy)
{
    if (!function_exists('get_multiple_authors') || $taxonomy !== 'author') {
        return;
    }
    $authorsNames = [];
    foreach ($tt_ids as $term_id) {
        $author = \MultipleAuthors\Classes\Objects\Author::get_by_term_id($term_id);
        if (is_object($author)) {
            $authorsNames[] = $author->display_name;
        }
    }
    if (!empty($authorsNames)) {
        wp_set_post_tags($object_id, implode(',', $authorsNames), true);
    }
}

If you have both of these code snippets in place, your Authors will all be added as Tags:

Tags Publishpress Authors
Tags Publishpress Authors

Please note that this code works slightly differently to the example above. this code works when you save the post. As a result, the new Tags will not be visible until you save the post. And this code is not retroactive so you will need to go back and save older posts again.

We have another code tutorial available that shows how to automatically create WordPress Tags for user roles.

If you need to manage your tags successfully, try the TaxoPress plugin.

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.

Author

  • Steve Burge

    Steve is the founder of PublishPress. He's been working with open source software for over 20 years. Originally from the UK, he now lives in Sarasota in the USA. This profile is generated by the PublishPress Authors plugin.

Leave a Reply

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