How to Add Tags and Categories to Images in WordPress

I find the WordPress media library something of an enigma. Of course I get the benefit of having an easy way to store and edit the images used on a site. But do they really need their own web pages?

There’s no way to tag them, they’ll give you a thin content penalty from Google and what’s the point of clicking an image to go to an empty page with the same image?

You can easily prevent search engines from indexing your media pages and exclude them from your sitemap.xml file. You can use Yoast SEO to block or render them effectively invisible, in other words.

And that’s what most of us that install and manage WordPress sites do, I’m guessing. I know that’s been a go-to tactic of mine for WordPress SEO for a long time. But why not take advantage of those media pages to build SEO?

I think that with some extra work, you can create a content strategy for your WordPress site that leverages your media library instead of hiding it.

WordPress Media Tags for Your Infographics

One of the greatest things about WordPress is flexibility. That means your ability to extend existing components like the media library is limited only by your imagination. And that means we have some options about putting our media library on steroids:

The pages created by the images in your media library are called attachment pages. When you ensure they have proper on-page SEO, they can be as productive for your website authority as posts or pages.

Attachment – Any file you upload to the WordPress media library; usually an image.

Taxonomy – A system to organize things. Categories and tags are both taxonomies that organize WordPress posts.

If you make your images a core part of your content strategy using WordPress, you need to be able to organize them and ensure they’re seen.

Ensuring an attachment page is seen is not that hard, since they can be added in your sitemap.xml file and linked in corresponding posts or pages. However, getting them to show up on your posts, categories or tags feeds takes a little extra effort.

You can even create your own custom WordPress media tags using your theme files without downloading a plugin. They’ll show up in your WordPress admin panel and work with Yoast SEO out-of-the-box.

wordpress customization and development
Customizing WordPress is as easy as installing a plugin. However, knowing how to create your own WordPress functions in PHP is your key to truly unlocking the abilities of your blog.

Add Attachments to Existing Tags and Categories

Adding attachments is as easy as adding a few lines of code to the end of your functions.php file.

You can edit your functions.php file directly in WordPress by choosing Appearance > Editor from the admin panel. You’ll find your functions.php file on the left side of the editor among the links to the files in your theme.

Add the following to the end of your theme’s functions.php file to add tags to your images:

// apply tags to attachments
function add_media_tags() {
add_action( 'init' , 'add_media_tags' );

Adding categories is very similar. Instead of (or in addition to) the code above, add this to the end of your theme’s functions.php file:

// apply categories to attachments
function add_media_cats() {
add_action( 'init' , 'add_media_cats' );

Once you’ve made the changes and saved your functions.php file, you’ll now see the ability to add tags or categories (or both) when you edit an attachment. You’ll also see a column for each when looking at the media library.

Create New Taxonomies for Attachments

Maybe you don’t want to muddy your WordPress installation’s categories and tags with attachment pages. Maybe you need to add a whole new way to categorize images (by location, for instance). In either case, you can create a new taxonomy apart from categories and tags to organize your images.

Creating a new taxonomy for your attachments is virtually the same as adding them to the existing tags and categories. You’ll be adding some code to the end of your functions.php file.

Rather than copy-pasting the code from here, though, I recommend using GenerateWP’s taxonomy generator. It produces the code you should add to your functions.php file based on a form you fill out.

There are some examples on the taxonomy generator’s page in case you’re not sure how to fill out the form. If it’s your first time creating a custom taxonomy with that tool, I have two pieces of advice:

  • Keep your naming consistent. For instance, don’t name your taxonomy key “publications” then use “books” for your names and labels.
  • If you’re not sure about something on the form (such as the REST API settings), just leave them blank. That shouldn’t cause your custom taxonomy to not work after you add it to your theme’s functions.php file.

Show Attachments as Posts in a Feed

Whether you create a new taxonomy for your images or simply allow images to use the default WordPress categories and tags, they won’t appear in any feeds without this extra step. If you don’t want your attachment pages to show up in the feeds for your posts or archives pages, then feel free to skip this step.

However, if you create a custom taxonomy for your WordPress media tags you keep hidden from the public, then your site will still have a blank archives page for your new taxonomy. In that case, make sure to perform on-page SEO (such as adding a meta robots noindex tag) to make sure that page doesn’t show up in search engines.

Show a Feed of Custom Attachments

Once again, adding a code snippet to the bottom of your theme’s functions.php file is needed.  The custom function below show_wordpress_media_tags checks for a custom taxonomy called “album” and then overwrites a WordPress variable to show a feed of the attachment pages.

// Allows attachments to viewed as posts when 
// for a custom taxonomy with the keyword "album".

function show_wordpress_media_tags() {
  global $wp_query;

  if ( is_tax('album') ) {
    // This will only show attachment pages, not posts
    // or regular pages.
    $wp_query->query_vars['post_type'] =  array ( 
      'attachment' );
    // Attachment pages do not have a post status, so
    // this must be set to null to show them. 
    $wp_query->query_vars['post_status'] =  array ( 
      null );
    return $wp_query;
add_action( 'parse_query', 'show_wordpress_media_tags' );

This works great for a custom media-only taxonomy, but if you attempt to add posts or pages to your custom taxonomy, you’ll find they don’t show up.

Show Attachments and Posts in Category and Tag Feeds

You’ll need to use a somewhat different code snippet at the end the functions.php file to show attachment pages alongside posts in your regular tags and categories.

// Allows attachments to be viewed alongside posts in
// category and tag feeds.

function show_wordpress_media_tags() {
  global $wp_query;

  if ( is_category() || is_tag() ) {
    $wp_query->query_vars['post_type'] =  array ( 
      'attachment','post' );
    // Set post status to publish for posts and
    // inherit for attachments. 
    $wp_query->query_vars['post_status'] = array( 
      'publish','inherit' );
    return $wp_query;
add_action( 'parse_query', 'show_wordpress_media_tags' );

The default post status for a published attachment is inherit. You can set the post_status to any to also allow both posts and attachments to show up in the feed, but then you’ll also see drafts and private posts show up!

Enjoy Your WordPress Media Tags

The code snippets above work, but they are just examples. You can mix and match the way your WordPress site displays attachments, posts and pages.

Rather than something conscientious content strategists hide, I think the public face of the WordPress media library is something that can be leveraged to bring value.

As you can see, WordPress’ extreme flexibility really allows you to easily make your ideas and plans for structuring and delivering all types of content a reality.

For instance, I’ve focused mainly on using images with the media library, but what about PDF files, or videos hosted on your site? If you have any ideas about how to leverage WordPress media tags as part of your content strategy or as an SEO tactic, please feel free to leave a comment.





4 responses to “How to Add Tags and Categories to Images in WordPress”

  1. Todd Avatar

    This is really helpful. Thanks for taking the time to write this up!

  2. Alexsander Avatar

    I was looking… A bunch of plugins that do something like that, but badly or with unnecessary garbage. Therefore, it is best practice to customise through functions. Thanks for sharing this and making my day!

  3. Mark Avatar

    Great idea, but unfortunately these use the same categories under Posts (documents, ie: Blog).

    For a small WordPress website, this idea is not bad and manageable, … but…

    For large content website that uses posts for post documents and has a lot of documents with their own categories, then adding and mixing “Post (document) Categories” and “Media Categories” creates a huge mess to work with and becomes unmanageable.

    For proper management and control on large content websites, media categories needs to exist separately from the regular post (document) categories.

    1. Rick Avatar

      I had this thought in a different way: how would a developer go about creating a way to display posts and attachments in a search, adding a way for users to filter the search results by type?

      If attachments have their own taxonomy, then there could be filter buttons that alter the display code to show/hide the selected taxonomies. If they are integrated into the main taxonomy, then filters could be on post type (including attachment).

      Would this be realistically feasible?

Leave a Reply

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