Automatically Create WordPress Tags from Author Information

Authors Tags 1

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 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);
}

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.


Leave a Reply

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

Professional publishing plugins for WordPress! Get PublishPress