Thread Starter
wp-Guy
(@wp-guy)
Anyone, please?
I’d like to replace the anchor tag with a div, is that possible?
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="http://localhost/wp-content/uploads/2008/06/large-image.jpg">
<div class="whatever"><img class="alignnone size-medium wp-image-24" title="image-name" src="http://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.
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: http://trac.wordpress.org/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:
http://trac.wordpress.org/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.