• Resolved wp-Guy

    (@wp-guy)


    I’m using WordPress 2.5.1

    By default, when I insert an image by using the “Add an Image” button in the “Write Post” page, the image gets inserted with the folowing code:

    <a href="SITE"><img class="alignnone size-medium wp-image-13" title="IMAGE" src="SITE" alt="" width="400" height="304" /></a>

    But I don’t want it to be a link. Instead of the link tag, I want to add a div with a class.

    Do you guys know if it is possible to modify something in the database to do what I need?

    Thanks in advance!

Viewing 10 replies - 1 through 10 (of 10 total)
  • Thread Starter wp-Guy

    (@wp-guy)

    Anyone, please?
    I’d like to replace the anchor tag with a div, is that possible?

    Moderator Samuel Wood (Otto)

    (@otto42)

    www.ads-software.com Admin

    Something along these lines would do it. Add this to your theme’s functions.php file.

    add_filter( 'image_send_to_editor', 'div_stuff', 10, 7);
    function div_stuff($html, $id, $alt, $title, $align, $url, $size ) {
    return "<div class='whatever'>$html</div>";
    }
    Thread Starter wp-Guy

    (@wp-guy)

    I’m just starting to learn PHP, so I don’t know how to use the information you just provided me.

    Thread Starter wp-Guy

    (@wp-guy)

    Could anyone help me, please?

    Not much to explain beyond what Otto already said…

    Does your theme have a functions.php file?

    If yes, edit it and copy/paste what he gave you before the final ?> at the bottom of the file.

    If no, create one. Start it with <?php then otto42’s code then ?>

    Thread Starter wp-Guy

    (@wp-guy)

    The last time I tried it didn’t work, but this time the div I want to add appears twice. Looks like this:

    <div class="whatever"><a href="https://localhost/wp-content/uploads/2008/06/large-image.jpg">
    <div class="whatever"><img class="alignnone size-medium wp-image-24" title="image-name" src="https://localhost/wp-content/uploads/2008/06/small-image.jpg" alt="" width="400" height="300" /></div>
    </a></div>

    I have two questions:
    How can I avoid having that div duplicated?

    I don’t want an anchor tag inside the div, how can I prevent it from being inserted automatically?

    Thanks in advance.

    Thread Starter wp-Guy

    (@wp-guy)

    Since the div “whatever” appears duplicated and I want to prevent the anchor tag from being inserted, I thought it would be good to try something else.

    From the code Otto42 gave me, I modified it to be like this:

    add_filter( 'image_send_to_editor', 'div_stuff', 10, 7);
    function div_stuff($html, $id, $alt, $title, $align, $url, $size ){
    return "<div class='whatever'>
    <img class='alignnone' title='$title' src='$url' alt='' width='400' height='300' />
    </div>";

    The problem with this code, is that the $url variable gives me the url for the large image, not for the medium sized image, which is the one I need.

    Thanks in advance.

    Moderator Samuel Wood (Otto)

    (@otto42)

    www.ads-software.com Admin

    That should not happen, so I took a look, and there’s a bug in 2.5.1 that creates the multiple div problem you’re seeing.

    Bug is documented here: https://trac.www.ads-software.com/ticket/6806

    It’s been fixed in the latest version of WordPress, and will be fixed in 2.6.

    To fix it right now, replace your wp-includes/media.php with this one:
    https://trac.www.ads-software.com/export/8188/branches/2.5/wp-includes/media.php

    Thread Starter wp-Guy

    (@wp-guy)

    Thank you very much, Otto42. It fixed the duplicated div issue.
    Now the only problem I have, is that I cannot remove the anchor tag inside the div.
    I’m starting to think that’s not possible.

    Thread Starter wp-Guy

    (@wp-guy)

    I solved the last problem. I was entirely focused on the div issue that I forgot to play with the options.

    Again Otto42, thank you very much for your help and for being so patient with a newbie like me.

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Modify code for inserted images, possible?’ is closed to new replies.