Plugin to tag (add a class to?) images attached to a post?

I’m looking for a plugin that will let a user tag images attached to a post. My goal is to have a class added to the tag so that I can (hopefully) then create different jQuery image rotators within that post, each using images with a different tag/class.

Edit with more detail:
I found a plugin called Media Tags, but I don’t think this (directly) accomplishes what I want. It adds a taxonomy to media items, and the media items can then be displayed on the site (through a template tag or a shortcode) according to their ‘media tag’. My ultimate goal is to make it possible for the person updating the site (a client with no previous knowledge of WordPress) to easily insert several images into a post, ‘tag’ them, and have those images displayed in separate jQuery image rotators within the post, according to the tag assigned to them. My initial thought is that the best way to do this is make it easy and intuitive for a class to be added to the tags, but I’m open to other suggestions.

I realize classes can be added to an already attached image by accessing the Edit Image > Advanced Settings screen, but I’m not sure how intuitive that will be, and I would of course prefer that the class/tag be chosen from a list, to minimize mistakes.

2 Answers
2

register_taxonomy_for_object_type('post_tag', 'attachment'); should do the trick. I think you could even do this from your themes functions.php .

Edit: ok, try this (save as attachmenttags/attachmenttags.php in your plugins folder and make sure WP can read it, then activate in plugin manager):

/*
Plugin Name: AttachmentTags
Description: enables tagging attachments
Author: Wyrfel <[email protected]>
Version: 0.1
*/

if (!class_exists('AttachmentTags') {
    class AttachmentTags {
        function AttachmentTags() {
            add_action('admin_init', (&$this, 'admin_init'));
            add_filter('wp_get_attachment_image_attributes', (&$this, 'add_tag_classes'), 10, 2);
        }

        function admin_init() {
            register_taxonomy_for_object_type('post_tag', 'attachment');
        }

        function add_tag_classes($attr, $attachment) {
            $tags = wp_get_object_terms($attachment->ID, 'post_tag', 'names');
            if (!empty($tags)) foreach ($tags as $tag) {
                $attr['class'] .= ' '.$tag; //wp sanitizes afterwards, so we don't need to
            }
            return $attr;
        }

    }
    $AttachmentTags = new AttachmentTags();
}

BTW: This also creates the post tags field in the add/edit image popup when editing the actual post/page.
Edited again, should now also inject the classes whenever you use get_the_post_thumbnail() in your theme.

Leave a Comment