WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

In regards to image attachments inserted into a post, I would like to wrap the img tag inside a div tag for specific design purpose.

Is there a way to do this automatically after attaching the image file, like through hooks/filters maybe?

Thanks in advanced!!

share|improve this question
up vote 4 down vote accepted

It's the image_send_to_editor filter:


  add_filter('image_send_to_editor', 'wrap_my_div', 10, 8);

  function wrap_my_div($html, $id, $caption, $title, $align, $url, $size, $alt){
    return '<div class="mydiv" id="mydiv-'.$id.'">'.$html.'</div>';

For existing images/posts you can try the regex in the function below

share|improve this answer
Sorry for the late reply. Been sidetracked. Anyway, thank you very much for this. Exactly what I'm looking for! Though funny thing is that when I delete the image, the div element seems to remain. Changing it to span seems to fix it. Cheers!! – Giraldi Jul 30 '11 at 16:07

Not looked into hooks and filters, but it is easily achievable by css alone.

Using the 2010 theme and class="entry-content" as the css selector

In your stylesheet use:

.entry-content img {  
/* do all your funky css attributes for the image in here */  
share|improve this answer
I'm aware of this method, but not really what I was after. I was trying to automatically wrap the image within another tag, eg. span, to achieve certain purpose. @one-trick-pony's solution above did the trick. Thanks for the reply, anyway. Cheers! – Giraldi Jul 30 '11 at 16:12

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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