Title: Modify code for inserted images, possible?
Last modified: August 19, 2016

---

# Modify code for inserted images, possible?

 *  Resolved [wp-Guy](https://wordpress.org/support/users/wp-guy/)
 * (@wp-guy)
 * [17 years, 11 months ago](https://wordpress.org/support/topic/modify-code-for-inserted-images-possible/)
 * 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](https://wordpress.org/support/users/wp-guy/)
 * (@wp-guy)
 * [17 years, 11 months ago](https://wordpress.org/support/topic/modify-code-for-inserted-images-possible/#post-785768)
 * Anyone, please?
    I’d like to replace the anchor tag with a div, is that possible?
 *  Moderator [Samuel Wood (Otto)](https://wordpress.org/support/users/otto42/)
 * (@otto42)
 * WordPress.org Admin
 * [17 years, 11 months ago](https://wordpress.org/support/topic/modify-code-for-inserted-images-possible/#post-785771)
 * 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](https://wordpress.org/support/users/wp-guy/)
 * (@wp-guy)
 * [17 years, 11 months ago](https://wordpress.org/support/topic/modify-code-for-inserted-images-possible/#post-785789)
 * 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](https://wordpress.org/support/users/wp-guy/)
 * (@wp-guy)
 * [17 years, 11 months ago](https://wordpress.org/support/topic/modify-code-for-inserted-images-possible/#post-785870)
 * Could anyone help me, please?
 *  [Chris_K](https://wordpress.org/support/users/handysolo/)
 * (@handysolo)
 * [17 years, 11 months ago](https://wordpress.org/support/topic/modify-code-for-inserted-images-possible/#post-785871)
 * 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](https://wordpress.org/support/users/wp-guy/)
 * (@wp-guy)
 * [17 years, 11 months ago](https://wordpress.org/support/topic/modify-code-for-inserted-images-possible/#post-785872)
 * 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](https://wordpress.org/support/users/wp-guy/)
 * (@wp-guy)
 * [17 years, 11 months ago](https://wordpress.org/support/topic/modify-code-for-inserted-images-possible/#post-785878)
 * 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)](https://wordpress.org/support/users/otto42/)
 * (@otto42)
 * WordPress.org Admin
 * [17 years, 11 months ago](https://wordpress.org/support/topic/modify-code-for-inserted-images-possible/#post-785879)
 * 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](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](http://trac.wordpress.org/export/8188/branches/2.5/wp-includes/media.php)
 *  Thread Starter [wp-Guy](https://wordpress.org/support/users/wp-guy/)
 * (@wp-guy)
 * [17 years, 11 months ago](https://wordpress.org/support/topic/modify-code-for-inserted-images-possible/#post-785880)
 * 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](https://wordpress.org/support/users/wp-guy/)
 * (@wp-guy)
 * [17 years, 11 months ago](https://wordpress.org/support/topic/modify-code-for-inserted-images-possible/#post-785881)
 * 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.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 10 replies
 * 3 participants
 * Last reply from: [wp-Guy](https://wordpress.org/support/users/wp-guy/)
 * Last activity: [17 years, 11 months ago](https://wordpress.org/support/topic/modify-code-for-inserted-images-possible/#post-785881)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
