How To Remove Paragraph Tags From Around Images In WordPress

In January, I posted how much I really hate WordPress’ automatic post formatting, and we will continue it today on this post. That tutorial removes automatic paragraph tags in WordPress, but what if we just want to remove them from images?

Here’s how you can remove paragraph tags from around images in WordPress to make them look exactly what you want. We can fix it with this snippet. Just drop the following snippet to your current theme’s functions.php file:

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');

Thanks to CSS Tricks for the snippet!

Leave a Reply

Your email address will not be published. Required fields are marked *