WordPress.org

Ready to get started?Download WordPress

Forums

Adding an extra class to post images (6 posts)

  1. jjpare
    Member
    Posted 3 years ago #

    I'm looking for a way to slip an extra class into all the img tags in a post. I thought I had tracked down the function that does it in the media.php file (the get_image_tag and get_image_tag_class filters), but I can't figure out what's going on in there. No changes I make to it seem to have any effect and there's nothing in the codex on how to wrangle either of those filters.

    Anybody have any ideas?

  2. riversatile
    Member
    Posted 3 years ago #

    Hi,

    You talk about this kind of class ? Imagine your post containers has the class called 'entries' :

    .entries img{
    border: 0;
    }

    In this example, it will affect all your images (only in your posts) and apply a border of 0 pixel.

  3. jjpare
    Member
    Posted 3 years ago #

    no, not quite. what I'm looking to do is to add a whole new class to the images in the HTML, not just manipulating what already exists with CSS.

    For example, in the HTML, WordPress adds this to an image tag:
    <img class="aligncenter size-large wp-image-129"...
    What I want to do is to add another qualifier to the image class called 'tilt'.

    You see, certain elements on the site (heads and images) get rotated slightly between -2 and 2 degrees. But the rotation is controlled randomly. I add a random number generator to the functions that control those elements that creates and adds a new series of classes between 'tilt0 and 'tilt5'.

    I've managed to do this for all the other elements I need to, but I can't figure out the function that adds those classes ('aligncenter', 'size-large', etc.) to the images. It does appear that the get_image_tag function in the media.php file is the one that does it, but I can't quite figure out how to take control of it. And it doesn't help that there is absolutely no mention of the get_image_tag filter anywhere in the codex, and that other forum queries on the subject have received almost no response whatsoever.

    If it makes any difference in the answer, what I'm working on is a child theme of Thematic.

  4. alchymyth
    The Sweeper & Moderator
    Posted 3 years ago #

    i played around a bit, and it seems, that the 'get_image_tag()' function (and the filter 'get_image_tag_class') in media.php only gets applied when an image is inserted into a post for the first time (not even if the image is updated)

    also:
    http://www.webtechwise.com/wordpress-filter-examples-changing-attributes-when-adding-images-to-posts/

  5. jjpare
    Member
    Posted 3 years ago #

    Alchymyth: Thank you!
    That was totally what I was looking for.

    I'm still at that stage where I can read and repurpose existing PHP and Java script, but not so much writing it from scratch. So this was a huge help. :)

  6. haloedrain
    Member
    Posted 2 years ago #

    "it seems, that the 'get_image_tag()' function (and the filter 'get_image_tag_class') in media.php only gets applied when an image is inserted into a post for the first time (not even if the image is updated)"

    Wow, thank you so much for mentioning that, I've been banging my head against this for a while and I don't know how much longer it would have taken me to figure that out myself. I haven't seen anything that talks about it mention that, so I'd assumed, since it's part of my theme, that it would apply when it displays.

Topic Closed

This topic has been closed to new replies.

About this Topic