Sunday, July 7, 2013

how to remove paragraph tags from around image tags in wordpress

If you want remove paragraph tags around pictures in wordpress
<p>blah blah blah</p>

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

<p>blah blah blah</p>

add this php code on functions.php file of your template :

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

add_filter('the_content', 'filter_ptags_on_images');

and you will get this result :

<p>blah blah blah</p>

<img src="monkey.jpg">

<p>blah blah blah</p>

Disqus Comments