Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 18th, 2012  |  syntax: None  |  size: 0.89 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <?php
  2. /*
  3. Plugin Name: Image P tag remover
  4. Description: Plugin to remove p tags from around images and iframes in content outputting, after WP autop filter has added them. (oh the irony)
  5. Version: 1.1
  6. Author: Fublo Ltd
  7. Author URI: http://blog.fublo.net/2011/05/wordpress-p-tag-removal/
  8. */
  9.  
  10. function filter_ptags_on_images($content)
  11. {
  12.     // do a regular expression replace...
  13.     // find all p tags that have just
  14.     // <p>maybe some white space<img all stuff up to /> then maybe whitespace </p>
  15.     // replace it with just the image tag...
  16.     $content = preg_replace('/<p>\s*(<a .*>)?\s*(<img .* \/>)\s*(<\/a>)?\s*<\/p>/iU', '\1\2\3', $content);
  17.     // now pass that through and do the same for iframes...
  18.     return preg_replace('/<p>\s*(<iframe .*>*.<\/iframe>)\s*<\/p>/iU', '\1', $content);
  19. }
  20.  
  21. // we want it to be run after the autop stuff... 10 is default.
  22. add_filter('the_content', 'filter_ptags_on_images');