Media tags plugin

Note: This post remains available to discuss the original 1.0 version of the Media Tags plugin. As of July 15 2009 version 2.0 of the Media Tags plugins has been released. Please refer to the update post http://www.codehooligans.com/2009/07/15/media-tags-20-released/ for information on changes to the use of the plugin.

Working on a client project recently I needed to extract 2 attachments uploaded from a post and display these along with the post title into the sidebar on another page. I’ve done this a few times before and always sad to say a little different. Probably the best solution is to name the ‘special’ attachments using some defined naming convention like image_sidebar_1.jpg, image_sidebar_2.jpg, image_sidebar_x.jpg. This works but can get ugly if you even want the client to follow your instructions on the naming conventions.

I’ve figured out a better way. With my latest plugin you can now add tags to your media attachments. I write ‘media’ because this does not just apply to images. You can tag Word document, PDF documents basically anything you upload via the standard WordPress Media upload tool.

Here are some screenshots:

The media tag input field is displayed directly on the media form.

Usage

So now you can tag you media files. But how do you actually use the tags? How to you actually access attachments that have ‘x’ tag? Excellent question. And your answer is below.

As part of the plugin I also wrote a function to be used in your template. To follow the example I started this article with I need to display 2 of the 5 uploaded images in the sidebar. My first step was to upload the images and tag the two special images with ‘sidebar’.

Now I use the function get_media_by_tag(). I pass the tag I want to access, in this case ‘sidebar’ and the ID of the post parent. What is returned is an array of attachment items. This array is similar in fact to the return object when calling the WordPress get_posts which is partly what the function does.

Download the Media-Tags plugin via the WordPress.org plugin repository

Future plans

I’ve already starting thinking of some enhancements for the plugin. Which include a tag management screen and other utility functions. If you have any other needs post them here in the comments.

About Paul Menard

Mis-placed Texas Geek now living on North Carolina. Lover of all things coding especially WordPress, Node.js, Objective-C and Swift. Love to work on interesting projects and come away with some new knowledge. Trying to keep my head on while I try to staying abreast of all the latest technologies. Lover of books and cats.