Skip to content

Instantly share code, notes, and snippets.

@pagelab
Created January 20, 2014 03:16
Show Gist options
  • Save pagelab/8514344 to your computer and use it in GitHub Desktop.
Save pagelab/8514344 to your computer and use it in GitHub Desktop.
<?php
/*
Plugin Name: Image P tag remover
Description: Plugin to remove p tags from around images and iframes in content outputting, after WP autop filter has added them. (oh the irony)
Version: 1.1
Author: Fublo Ltd
Author URI: http://blog.fublo.net/2011/05/wordpress-p-tag-removal/
*/
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...
$content = preg_replace('/<p>\s*(<a .*>)?\s*(<img .* \/>)\s*(<\/a>)?\s*<\/p>/iU', '\1\2\3', $content);
// now pass that through and do the same for iframes...
return preg_replace('/<p>\s*(<iframe .*>*.<\/iframe>)\s*<\/p>/iU', '\1', $content);
}
// we want it to be run after the autop stuff... 10 is default.
add_filter('the_content', 'filter_ptags_on_images');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment