Automatically wrap post image in div

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?

Read More

Thanks in advanced!!

Related posts

Leave a Reply

2 comments

  1. 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

  2. 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 */  
    }