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!!

2 Answers 2


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

  • 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, 2011 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 */  
  • 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, 2011 at 16:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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