Posted By

rliverman on 12/22/11


Tagged

php wordpress images wrap wpautop wpauto


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

100tral
Terion
Priestd09


Stop Wordpress From Wrapping Images in Paragraph Tags


 / Published in: PHP
 

Wordpress by default wraps images in

tags, this kills that.

  1. // stop wordpress from wrapping images in <p> tags
  2. function filter_ptags_on_images($content)
  3. {
  4. // do a regular expression replace...
  5. // find all p tags that have just
  6. // <p>maybe some white space<img all stuff up to /> then maybe whitespace </p>
  7. // replace it with just the image tag...
  8. return preg_replace('/<p>\s*(<a .*>)?\s*(<img .* \/>)\s*(<\/a>)?\s*<\/p>/iU', '\1\2\3', $content);
  9. }
  10.  
  11. // we want it to be run after the autop stuff... 10 is default.
  12. add_filter('the_content', 'filter_ptags_on_images');

Report this snippet  

You need to login to post a comment.