2

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
  • By a "user", do you mean a user in WordPress that can edit posts etc., or a visitor to your site? In general, are we talking about tagging from the post editor or from the frontend page?
    – Jan Fabry
    Feb 17, 2011 at 14:06
  • Sorry for the ambiguous wording. I mean someone tagging from the post editor. Feb 17, 2011 at 14:47

2 Answers 2

1

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.

5
  • What should this result in? I tried that code, but don't see any changes. Feb 17, 2011 at 13:13
  • There's code in the function that creates the media editor interface (that comes up if you click on an attachment in your library) that checks for available taxonomies for attachments and should create a field for each available taxonomy. If above doesn't work, i either have an error in there (tag instead of post_tag, maybe?) or WP still ignores it. Are you calling it from within the 'init' or 'admin_init' hook?
    – wyrfel
    Feb 18, 2011 at 3:46
  • Just tested it, it definately works as stated above if you run it from within your 'admin_init' callback. Actually, it's possible that if you do this from within your themes functions.php, that that doesn't get called in admin at all.
    – wyrfel
    Feb 18, 2011 at 3:49
  • I tried saving this as a plugin like you suggested, but still no luck. WP can see it, but throws an error when trying to activate it: Parse error: syntax error, unexpected '{' on line 9 That's not the only syntax error though, and I'm not sure how to fix all of them. Mar 7, 2011 at 15:45
  • @tnorthcut ahh sorry, it's a stupid typo i do way too often...i had () behind the class definition. Removed it.
    – wyrfel
    Mar 8, 2011 at 10:23
1

I recommend you use Attachments plugin. This adds a small meta box to the bottom of a post, where the user can easily add images from the library or upload new images. If you use add_image_size you can add support for any sizes your themes needs. They can also re-order the images by drag and drop and add metadata such as caption and alt text.

You can integrate this into your theme really easily, images will always be the right size and you can then add something like jQuery cycle to add rotating images on each post.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.