Media tags plugin
Filed Under: Tags: media-tags, plugin, WordPress
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:
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.
1 2 3 4 5 6 7 | $media_items = $mediatags->get_media_by_tag('media_tags=sidebar&post_parent=6'); This call will filter the tags for the tag 'sidebar' and the post ID of 6. The function take three arguments. media_tags (Required) This is the tag or tags you are filtering on. media_types (Optional) Lets you also filter by pdf, txt, etc. post_parent (Optional) The ID of the attachment post. If not provided then the global post ID is assumed. |
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.
Both comments and pings are currently closed.






December 14th, 2008 at 3:37 pm
I think a really nice addition to this, including your future plans of a tag management screen, would be to integrate it, or atleast the idea of Matt’s Community Tags, to make a more enhanced version of his photo tagging. Great plugin though!
December 14th, 2008 at 3:48 pm
@Shane: Thanks for the comment. I downloaded and installed Matt’s plugin but having a hard time figuring out what it does. Regardless it reads like the goal of his plugin is to allow community tagging. My plugin is strictly for wp-admin users use.
December 16th, 2008 at 1:51 pm
[...] Media Tags [...]
December 17th, 2008 at 11:40 am
[...] Media Tags [...]
December 30th, 2008 at 6:46 am
[...] Media Tags [...]
January 7th, 2009 at 10:55 am
[...] Media Tags [...]
January 8th, 2009 at 1:16 am
Thank you – this plugin is immensely useful. WordPress’ templates and tags have yet to catch up with the media features they’ve added.
January 28th, 2009 at 3:22 pm
Just what I was looking for (and could not find)! I need to ‘categorise’ sports club documents eg. into results, entry forms etc. Perfect…
January 28th, 2009 at 3:32 pm
@Sports Club Design: Dude that is the exact reason I wrote this plugin. Thanks!
February 22nd, 2009 at 1:32 pm
I was delighted to find a plugin to improve the native image management of the WordPress media library. However, what I’m really looking for is a plugin that would allow me to filter the content of the media library according to the tags both in the media library and in the pop-up on “Add Post”. Ist that kind of thing planned for one of the future releases of this plugin?
February 22nd, 2009 at 6:42 pm
@Juno:
Yes that is one of my future plans on this plugin. As of the current version the tags are only displayed for the current post/page item relationship. The next version (not sure when) will allow you to search/filter by tag and display those media items matching the tag(s). I’ve just returned from a nice vacation so look for the new plugin version in the coming weeks.
February 25th, 2009 at 8:18 pm
i have installed the plugin, and added a tag of ‘photomedia’ to an image in the media library.
i added the code below to a page template, but the image is not pulled in. am i missing anything here?
get_media_by_tag(‘media_tags=photomedia’); ?>
thanks
February 25th, 2009 at 8:20 pm
sorry not sure if my code is displaying properly… [code] get_media_by_tag('media_tags=photomedia'); ?> [/code]
February 25th, 2009 at 8:22 pm
no its not…. im wrapping php tags around what is listed above in the usage section, with the only argument being media_tags=photomedia
February 26th, 2009 at 8:49 am
@Rhys:
When using the ‘get_media_by_tag’ function you need to include the object reference like this:
$my_images = $mediatags->get_media_by_tag(‘media_tags=photmedia’);
This function call returns an array. Each array item is the child attachment to the given post_parent. You will need to process the array in your own template code.
March 6th, 2009 at 12:03 pm
I’m happy to hear the holiday was so nice. Good news for plugin-fans.
March 7th, 2009 at 4:06 pm
Anyone have luck getting this working with 2.7.1? I’m getting – Fatal error: Call to a member function on a non-object in …when trying to use: $img = $mediatags->get_media_by_tag(‘media_tags=sidebar’);
March 7th, 2009 at 4:13 pm
solved my own question: use global $mediatags; if your in a function. great plugin. thx.
March 8th, 2009 at 1:24 pm
@brian: Glad to hear you figured this out. Using the ‘global’ declaration when calling the object from within our own function is standard in the PHP language. Also remember that if you use the object in your sidebar, header or footer you will need to do the same since you generally will access these template file using the WordPress built-in function get_sidebar(), get_header() and get_footer. These WordPress functions in turn will load or include the needed sidebar.php, header.php and footer.php template files.
March 14th, 2009 at 12:26 am
[...] an input to the media upload and management screens… …..read more Download Plugin! Plugin Owner: Paul Menard Homepage: Visit Plugin’s Website Version 1.0.1 [...]
March 15th, 2009 at 5:16 pm
Cool!
April 30th, 2009 at 11:16 am
I am still confused as to how to actually get the function to output on the page. Do I need to put echo $media_tags; underneath my function call?
April 30th, 2009 at 11:32 am
Jeremiah,
Have you read the post above on my site. I give the exact template you will need to add to your current template to have it display. The $media_tags is an object reference to the plugin. so echoing it will not provide anything. Here is some code for you to use:
// In the line below I’m calling media_tags looking for any image tagged ‘thumb’ for the current post. Yes this is from inside the Loop
$media_items = $mediatags->get_media_by_tag(‘media_tags=thumb&post_parent=’.$post->ID);
// If something was returned. The returned object is an array of the ‘thumb’ images.
if ($media_items)
{
// Here I’m assigning ‘attachment_url’ the URL of the first image from media_items.
$attachment_url = wp_get_attachment_url($media_items[0]->ID);
}
// After this point I can display the attachment_url as the src value of an image element.

April 30th, 2009 at 4:41 pm
Paul,
That helps clarify (not a PHP programmer). I am giving this a go. Thank you.
May 4th, 2009 at 9:55 am
I think the plugin is great! I have been looking for something like this for ages!
I would recommend that the tag system works like the one for wordpress, so that you can choose from a list instead of organized columns. Furthermore, I think it would be cool if it was possible to add the tags beneath the pictures that is displayed, however, I cannot find the code to implement this. I realize that it is displayed here on this site, but it would be easier if it had come along with the information for the plugin or had its own post here on your site.
With future updates it would be nice it it is possible to add tags for multiple pictures. If for instance the pictures are of some sight or displays the same thing.
Again a great plugin that really should be implemented in WP by default!
Thank you so much!
May 14th, 2009 at 1:09 am
Hello Paul.
Great plugin you have writed
I would like to know if the actual version is able to generate a tag page in order to sort the tagged media. As WordPress already do indeed.
Thank you
Laetitia
May 14th, 2009 at 7:11 am
Hi Laetitia, Thanks for the comment.
At the current released version the plugin does not provide a tag page or listing of all media tags used through the site. It’s on my list of upcoming enhancements. Stay tuned.
May 28th, 2009 at 10:55 pm
Very nice plugin you’ve made Paul… I’m eager to see the next version released, as the search/filter will be really useful
May 29th, 2009 at 1:12 pm
This is brilliant!
June 25th, 2009 at 11:20 am
So excited to try this out! My little two cents of what would be great is a mass-edit screen where we can easily apply tags to multiple images.
Thanks so much for what I’m sure will be an awesome plugin!
June 30th, 2009 at 7:20 pm
i am so sorry to be annoying but i have spent hours on this and am completely confused.
i have tagged two images in two different posts with the tag “test tag”.
I want to build a new page template where the sole display item is the images with the tag “test tag”.
So, I’m not in a function when I call the $mediatags. Which means i have put in this code inside my php tags:
$media_items = $mediatags->get_media_by_tag(‘media_tags=test tag’);
If you could let me know what I’m doing wrong, I would greatly appreciate it!
Thank you!
June 30th, 2009 at 9:11 pm
@Ami:
Thanks for the comment. Unfortunately building a page template just for a media tag is not possible. In the current release of the plugin the media tags are effective only for the given post item. So this means you can only call the media tags function from within the loop. I’ve been working on a new version of the plugin that will have a Media Tags management page much like the Categories and Tags WordPress sections. Sadly I have too much client work going and need to focus on paying bills. Hope to get this new version out soon.
July 1st, 2009 at 8:37 am
@Paul Thank you so much for responding – I thought I was going crazy!
I really appreciate your work on this plugin and will definitely donate when I can get it working for my needs.
Warmest,
Ami
July 1st, 2009 at 8:07 pm
ok, this is my last comment! i hope this helps someone else because i got this to work outside the loop using a select query. I hope this is going to display correctly! everything is inside a php tag.
$tagimages = $wpdb->get_results(“SELECT wposts.*
FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
WHERE wposts.ID = wpostmeta.post_id
AND wpostmeta.meta_key = ‘post_media_tags’
AND wpostmeta.meta_value = ‘escort cards’
ORDER BY wposts.post_date DESC”);
if ($tagimages) :
foreach ($tagimages as $post) :
$imgtag = $post->guid;
$postpid = $post->post_parent;
?>
<a href=”"><img src=”/scripts/timthumb.php?src=&h=150&w=150&zc=1″>;
July 1st, 2009 at 8:44 pm
Ok this is my last post also.
Ami Thanks for the code. I’m sure others who read these comments will find them usefull. While your code does in fact work I do not advocate anyone hand-coding SQL queries into their templates.
Wound not a better solution be to use the build-in ‘get_posts’ WP function?
$args = array(
‘post_type’ => ‘attachment’,
‘numberposts’ => -1,
‘meta_key’ => ‘post_media_tags’,
‘meta_value’ => ‘page-full’
);
$tagimages = get_posts($args);
I just tried this on a client site using a ton of media-tagged images and came up with the same results when compared to your direct SQL.
One other not about using the post meta fields. This is going away in the next version of the Media Tags plugin I’m working on. I’ve converted all the post meta fields to using the WordPress Taxonomy system just like Categories and Tags. In that case it would be easier to just query the terms for all the posts/pages for a given term. Then spin the array.
Made some great progress on the plugin this afternoon. At least over 2 major hurdles. Hope to have version 2.0 out this month early. Actually my birthday is July 13 so this will probably be on that day.
July 1st, 2009 at 9:13 pm
@paul
thanks – you are absolutely right – i too get the same results! (i am not a php coder, but i do know sql, hence my foray into what was clearly not optimal).
can’t wait for the new version, you ROCK.
July 2nd, 2009 at 5:14 pm
Hi Paul,
Thanks for your work. I just wanted to suggest the implementation of a shortcode to ease the display of related media on the posts. Regards!
July 2nd, 2009 at 8:56 pm
@Carlos: Thanks for the comment. Yeah shortcodes with lots of options are on my list of features on the next version. But thanks for the suggestion. Love getting suggestions and request from user. Goes for anyone else since I’m working on this plugin preparing for the next release if you have any suggestion please feel free to post them here. I’ll see what I can do about getting the requests addressed.
Already on my list and in development:
1. Move the media tag storage to the WordPress Taxonomy system and stop using postmeta table for lookup. This will provide tons of new flexibility for queries.
2. Add a Media Tags admin screen. This is already added. There will be a new panel under Media menu section. The screen looks and functions just like the WordPress Tags/Categories management screen.
3. Shortcodes for drop into content easy of use. This is working as of this week.
4. Add many more template type codes that all queries across all Posts/Pages for media tags.
5. Standardize on the template function parameters to be more like the WordPress get_posts parameters. Allowing parameters like numberposts, orderby, offset, etc. This will allow you to select media tag items and use a simple paging type system. More on that later.
6. Add functionality to return Posts instead of the attachments. In the current media-tags version only the post attachment(s) are returned. Many need the ability to query display posts which have attachments matching some media-tag slug.
7. Media Tag cloud template function and widget for your page/sidebar use.
8. Search functionality from within Media section. A way to search from within Media or the Media popup to filter items. This may not make it into the 2.0 release.
9. Query on attachment sizes. This would be neat to have. Just not sure if it would be something everyone needs.
10. (open)
Also in a few weeks I’ll be hunting some beta testers for the 2.0 version of the plugin. I would not suggest running this on a production system. Thinking more if you are made happy using the current Media Tags plugin and have access to a dev instance of your site f might help to have some of you test the new features.
July 7th, 2009 at 10:01 am
Ami, Paul, thanks for posting code! Really helpful.
July 8th, 2009 at 4:35 am
Hey Paul, did you get to step 1 on your todo list yet? This is the killer feature for me. If you did, any chance of a prelease version? I am in need of this pretty badly right now. Keep up the great work.
July 8th, 2009 at 7:36 am
@Moth Menace: Yes I have made it through most of my list. Stuck on the Media Tag Cloud which I might just pass on for the moment. The backend functionality is wrapped. Will be announcing a beta release hopefully today in a new post on this site. Stay tuned…
July 8th, 2009 at 3:29 pm
All I’ve finally reached the point in the development of the new Media Tags plugin to go public. Public beta that is. I’ve started a new post http://www.codehooligans.com/2009/07/08/media-tags-20-beta/ where you can download the new 2.0 Beta A version. There I’ve also listed many of the key features in this latest release. Feel free to download this and provided feedback. I always appreciate it. Thanks.
July 15th, 2009 at 11:53 am
Comments are now closed on this Post. Please refer to the official Media-Tags 2.0 Release post where you will be able to enter comments. http://www.codehooligans.com/2009/07/15/media-tags-20-released/