Stop Wordpress From Wrapping Images in Paragraph Tags


/ Published in: PHP
Save to your folder(s)

Wordpress by default wraps images in <p> tags, this kills that.


Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.