Lego2012
12/13/2016 - 11:15 AM

Remove Paragraph Tags From Around Images

Remove Paragraph Tags From Around Images

<!-- http://blog.fublo.net/2011/05/wordpress-p-tag-removal/ -->

<!-- In case you want to have <img> in your content but not have them get "Auto P'd" like WordPress likes to do. -->

<!-- example of problem: -->

<!-- blah blah blah -->

<img src="monkey.jpg">

<!-- blah blah blah -->

<!-- turns into: -->

<p>blah blah blah</p>

<p><img src="monkey.jpg"></p>

<p>blah blah blah</p>

<!-- We can fix it with this: -->

function filter_ptags_on_images($content){
   return preg_replace('https://cdn.css-tricks.com/<p>\s*(<a .*>)?\s*(<img .* \/>)\s*(<\/a>)?\s*<\/p>/iU', '\1\2\3', $content);
}

add_filter('the_content', 'filter_ptags_on_images');

<!-- For your functions.php file, or, see Reference URL for a plugin. With this in place, we get: -->

<p>blah blah blah</p>

<img src="monkey.jpg">

<p>blah blah blah</p>

<!-- ... meaning things like floating the images will be much easier. -->