Wordpress

How to Apply Custom Taxonomies, Tags and Categories to Attachments

Although WordPress doesn’t allow you to assign tags or categories to your uploaded media, it’s extremely easy to do so. Not only that, you can even create a custom taxonomy in just 10 minutes!

You will need:

  • A development installation of WordPress
  • A text editor
  • FTP access to create and upload the plugin OR access to the theme’s functions file (I would recommend using the plugin to add this functionality so that it will be available to you even if you change the theme)

Assigning categories

As you can see below, the Media Editing screen is quite bare. It doesn’t have any option to add categories, tags or taxonomies.

categories tags taxonomies media editing screen

You can change that. To add categories, use the register_taxonomy_for_object_type() function. Add the following code in your theme’s functions file or plugin file:

1

2

3

4

function wptp_add_categories_to_attachments() {

register_taxonomy_for_object_type( ‘category’, ‘attachment’ );

}

add_action( ‘init’ , ‘wptp_add_categories_to_attachments’ );

 

After saving the file, refresh the Media Editing screen and you will see that categories can now be added.

categories tags taxonomies media editing screen with categories

A categories column is also added to the Media Library screen.

categories tags taxonomies media library screen with categories

Assigning tags

You can also add tags using the same method. Just add the following code to your plugin file or theme’s functions file:

1

2

3

4

function wptp_add_tags_to_attachments() {

register_taxonomy_for_object_type( ‘post_tag’, ‘attachment’ );

}

add_action( ‘init’ , ‘wptp_add_tags_to_attachments’ );

 

Like before, the tags function will be added to the Media Editing screen.

categories tags taxonomies media editing screen with tags

The Media Library screen will also show a column for tags.

categories tags taxonomies media library screen with tags

Creating a custom taxonomy

Sometimes, you might not want to use the existing tags or categories and wish to create a custom taxonomy.

For example, in the picture above, I used a location tag to show where the image was taken. If you want to create a ‘Locations’ taxonomy to organize and display the pictures, you can use the register_taxonomy() function. Add the following code to your theme’s functions file or plugin file:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

// register new taxonomy which applies to attachments

function wptp_add_location_taxonomy() {

$labels = array(

‘name’              => ‘Locations’,

‘singular_name’     => ‘Location’,

‘search_items’      => ‘Search Locations’,

‘all_items’         => ‘All Locations’,

‘parent_item’       => ‘Parent Location’,

‘parent_item_colon’ => ‘Parent Location:’,

‘edit_item’         => ‘Edit Location’,

‘update_item’       => ‘Update Location’,

‘add_new_item’      => ‘Add New Location’,

‘new_item_name’     => ‘New Location Name’,

‘menu_name’         => ‘Location’,

);

 

$args = array(

‘labels’ => $labels,

‘hierarchical’ => true,

‘query_var’ => ‘true’,

‘rewrite’ => ‘true’,

‘show_admin_column’ => ‘true’,

);

 

register_taxonomy( ‘location’, ‘attachment’, $args );

}

add_action( ‘init’, ‘wptp_add_location_taxonomy’ );

 

This code will create a hierarchical ‘Locations’ taxonomy, which can only be used with attachments.

If you want to use it with other content types, you will have to change the register_taxonomy() function. Instead of the ‘attachment’ parameter, add an array which contains the slugs of the content types you want to add, like this:

1 register_taxonomy( ‘location’, array( ‘attachment’, ‘post’ ), $args );

 

The new taxonomy can now be seen in the Media Editing screen.

categories tags taxonomies media editing screen with custom taxonomy

Since I added the ‘show_admin_column’ argument, the taxonomy will be shown in the Media Library screen too.

 categories tags taxonomies media library screen with custom taxonomy

And you’re done! See how easy it is to assign categories, tags and custom taxonomies to attachments and other content types.

About the author

Tabish Shaikh

Website designer, website developer, blogger and a writer. These are the things I like, love and live for. Having years of experience in each field. Developing sites since childhood. Getting the most complicated things done is my specialty. Providing each and everything of top most quality to each and every client.

Leave a Comment


*