Wordpress Plugins – Extensions

Just another WordPress.com weblog

Media Tags

Author: Paul Menard
Plugin Homepage

Upload the extracted plugin folder and contained files to your /wp-content/plugins/ directory
Activate the plugin through the ‘Plugins’ menu in WordPress.
If you are upgrading from an earlier version of Media Tags the tags will be converted automatically when the plugin is activated.
Navigate to any Post/Page where a media file has been attached. Click on the ‘Add Media’ option on the post content icon bar. Select a media item from the Gallery. Click the ‘Show’ link to show the media item details. You will notice the new ‘Media Tags’ input field just below the WordPress Size option.
All used tags will be displayed as checkboxes below the new Media Tag input field.
Also all used media tags can be managed via the new Media Tags interface as part of the Media section in wp-admin. Management of Media tag is now part of a familiar interface like Tags and Categories.
Tweak out your template to display media tags (optional).

Ok I can now add tags to my images. So what, what can I do with them

Excellent question. By default if you are uploading an image to a post then inserting the media link into your post there is no need for you to use this plugin as it will not provide any benefit for you. But consider this scenario. You have 5 images you want to upload for a post. Two of these images will be inserted into the actual post content but the other three you want to display in the sidebar. How would you do this? Normally, in the past I would make the 3 images used in the sidebar follow a specific filename convention like sidebar1.jpg, sidebar2.jpg and sidebar3.jpg. This get ugly if you are managing lots of images. I would be better ( and one reason I wrote this plugin) to allow the tagging of the 3 images as ‘sidebar’.

To actually get to the images for display in the sidebar you will need to have a way of filtering the attachments that are associated with a post. Luckily I’ve also included a simple function: get_attachments_by_media_tags() This function works similar to most other WordPress functions.

There are a number of arguments that can be passed into the function to filter the media:

media_tags: (Required) This is a comma separated list of tags (slug) you want to filter on.

media_types: (Optional) This is a comma separated list of media types – gif, pdf, png to return

search_by: (Optional) Default is ‘slug’. Denotes what is passed in the ‘media_tags’ parameter. To search by media tag name pass the name into the ‘media_tags’ parameter and ‘name’ into the ‘search_by’ parameter.

post_parent: (Optional) This is the post ID of the related media items. This item is no longer required. This allows you to query media_tag items across all posts

numberposts: (Optional) Default is all. Allows control over the number of items returned.

orderby: (Optional) Default menu_order. See get_posts() for full list of options

order: (Optional) Default ‘DESC’. Controls the order of items. Other option is ‘ASC’.

offset: (Offset) Default is 0. Allows control over the subset of items returned.

post_type: (Optional) Now that WP supports custom post types you can now filter based on your custom post type. Default is all posts types.

nopaging: (Optional) Similar in functionality to the get_posts ‘nopaging’ argument.

return_type: (Optional) Defaults to Array. Other option is ‘li’. In the case of shortcodes the return type is ‘li’ by default. When using the ‘li’ return type each element is given a class of ‘media_tag_list’ and an id of ‘media-tag-item-xxxx’ where ‘xxx’ is the attachment id.

tags_compare: (Optional) Defaults to ‘OR’. When requesting multiple media_tags elements allow a compare between the lists of items returned. When using the ‘OR’ option returned item will be in one or more of the requested media_tags values. Other value is ‘AND’. When using ‘AND’ you are requesting only attachments which are in all requested media_tags.

Actual examples of the function call are:

$media_items = get_attachments_by_media_tags(‘media_tags=banner’);
This call will filter the tags for the tag ‘banner’.

$media_items = get_attachments_by_media_tags(‘media_tags=sidebar,trees&media_types=pdf,xls&tags_compare=AND);
This is another example. Filtering media based on the tags sidebar and trees. Also filtering the media type to be pdf or xls only. We are also specifying the ‘AND’ compare option to only return elements that are part of all media_tags.

Note: in the earlier versions of the media tags plugin you had to call the function by way of the object like


The use of the object as well as the ‘get_media_by_tag’ function has been deprecated. In the current version you can now access the function directly as in ‘get_attachments_by_media_tags(…)’. The legacy $mediatags->get_media_by_tag() function is still supported for now.

I want to use the new shortcode feature to display attachments by media tag on a page

Great. The media tags plugin now support the use of shortcodes. In its simplest form the media tag shortcode appears like

[media-tags media_tags=”alt-views,page-full,thumb”]

Note you need to include quotes around the element value to ensure proper handling. The shortcode parameters available are pretty close to the direct function call. The media_tags parameter is the mediatag slug. The exception is you need to specify the parameter name and value pairs.

[media-tags media_tags=”alt-views,page-full,thumb” tags_compare=”AND” orderby=”menu_order”]

When using the shortcode option the return type is automatically set the ‘LI’ which means list elements will be returned. When using shortcodes you can specific two parameters ‘before_list=’ and ‘after_list=’ to control the wrapper elements. Also, you can specify a new parameter ‘display_item_callback=’. This parameter will be a user defined function which allows you to control the output. The callback function will be called for each found element. Check the plugin file ‘mediatags_shortcodes.php’. There is a function used ‘default_item_callback()’ which is the default callback actually used by the plugin. Remember when using shortcodes in WordPress you MUST return every output. Do not echo information.

Lastly, when using the shortcode feature you may have a time when you need to specify the post_parent for the media_tags queried. Now you could add the post_parent ID directly to the shortcode settings. But then you would be hard-coding the ID for the given post. A better way is to just set the post_parent value to ‘this’. This will allow dynamic control over the shortcode processing.

[media-tags media_tags=”alt-views,page-full,thumb” post_parent=”this” before_list=”<ul class=’frank’>” after_list=”</ul>”]

Note as of version 2.1.3 of the plugin the ‘media_tags’ parameter must be the slug. To search by name use the new ‘search_by=’ parameter. As follows:

[media-tags media_tags=”Italy” search_by=”name” post_parent=”this” before_list=”<ul class=’frank’>” after_list=”</ul>”]

I want to display the media tags in my sidebar

You can use the new template function ‘list_mediatags()’. This will provide a list of all media tags used in your system. Much like the WordPress list_tags() function.

There are many more template functions available. Check out the functions listed in the ‘mediatags_template_functions.php’.

I want to display listings of media tags like an archive. How can I do that?

The new version (2.x) now support the use of a new permalink structure for displaying media tags archives. This is automatic when you activate the plugin. A simple way to see this in actual is to add the template function ‘list_mediatags()’ to your sidebar. This will list all media tags in your system complete with clickable links. When you click on one of the links you will see the URL is something like:

http://www.yoursite.com/media-tags/&lt;media tag slug>

By default the plugin will use the default WordPress templates in your theme directory. This means is you have a template file ‘archive.php’ it will be used to display the archive. If not then the ‘index.php’ template file will be used. Optionally, you can also use the new mediatags.php template file in your theme. This will let you control the display of media tags archives from other post archives. Also, you can create media tag specific template files like ‘mediatag-xx.php’ where ‘xx’ is the media tag ID. This follows similar convention for the built-in WordPress category template hierarchy. http://codex.wordpress.org/Category_Templates

Media Tags


July 17, 2010 - Posted by | download, extension, extensions, free, get, internet, plugin, plugins, Uncategorized, wordpress

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: