Assign Categories to Media Files

On WordPress you can upload numerous types of files to your “Media” section. Besides attaching those files with your posts, you can upload files and assign any registered taxonomy terms, just like the way you would assign categories to your posts.

By default WP does not provide such feature, however there are functions that will you to do exactly what I just did. This could be particularly very useful if you want to organize your files by category. Here is the snippet.

<?php  
// add categories to attachments 
function ib_add_categories_to_attachments() {
   register_taxonomy_for_object_type('category','attachment'); }  
add_action('init','ib_add_categories_to_attachments'); 
?>

Like always, simply copy this snippet and paste it on your current theme’s functions.php page and update it. You are all set. You should see the “categories” menu item underneath “Media” menu.

Note: Let’s not forget that we are using an already registered (for Posts) taxonomy (category) here. You can achieve the same result simply by registering a custom taxonomy for your “attachment” post type just in case if you do not want to touch your default post “category” taxonomy.

Got questions? Feel free to use the comment section below.

Today In History

  •  
  •  

Comment

Leave a Reply

Note: Convet HTML, PHP, JavaScripts from Postable, before posting from comment section.
License: By submitting a comment here you grant this site a perpetual license to reproduce your words and name/Web site in attribution. Please use your real name or a pseudonym (i.e., pen name, alias, nom de plume) when commenting. If you add your site name, company name, or something completely random, I'll likely change it to whatever I want.