Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

2 Answers 2

up vote 3 down vote accepted

It's the image_send_to_editor filter:

if(is_admin()){

  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!! –  gmaggio 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! –  gmaggio Jul 30 '11 at 16:12

Your Answer

 
discard

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.