8/18/2013 - 3:48 PM

Stop WP from wrapping <img> in <p> tags

Stop WP from wrapping in


<?php/*Plugin Name: Image P tag remover
Description: Plugin to remove p tags from around images in content outputting, after WP autop filter has added them. (oh the irony)
Version: 1.0
Author: Fublo Ltd
Author URI: http://fublo.net/*/
function filter_ptags_on_images($content){    
  // do a regular expression replace...    
  // find all p tags that have just    
  // <p>maybe some white space<img all stuff up to /> then maybe whitespace </p>    
  // replace it with just the image tag...    
return preg_replace('/<p>\s*(<a .*>)?\s*(<img .* \/>)\s*(<\/a>)?\s*<\/p>/iU', '\1\2\3', $content);
// we want it to be run after the autop stuff... 10 is default.
add_filter('the_content', 'filter_ptags_on_images');
//removes <p> tags if they contain only images. Leaves the <a> tags around <img> tags intact