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

I searched for all kind of solution but i really could find one that did what i was looking for, what i simple want done is wrapping all images inside a div element, old post aswell as new posts. All help is very much appreciated

p.s. I did a little jquery snippet but I really would like another solution

the jquery code

    $('a:has(img.postthumbimage)').wrap('<div class="myphoto" />');
share|improve this question
Could you post the code that outputs your images? – Joshua Abenazer Dec 9 '11 at 16:05
up vote 4 down vote accepted

Try using PHP's regular expression find/replace function—preg_replace()—in a filter on the the_content hook.

function breezer_addDivToImage( $content ) {

   // A regular expression of what to look for.
   $pattern = '/(<img([^>]*)>)/i';
   // What to replace it with. $1 refers to the content in the first 'capture group', in parentheses above
   $replacement = '<div class="myphoto">$1</div>';

   // run preg_replace() on the $content
   $content = preg_replace( $pattern, $replacement, $content );

   // return the processed content
   return $content;

add_filter( 'the_content', 'breezer_addDivToImage' );

Note that this will only affect images inside post content.

If you want to wrap images that occur outside of the post body (such as featured images or images in custom fields), the most efficient way is probably to find where they occur and add the div wrapper to your theme file.

share|improve this answer
This solution doesn't work when you have a caption in your image – Richard Lindhout Dec 2 '15 at 14:41

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.