Remove paragraph tags from images

Paragraph tags around images can sometimes cause styling issues. If you want to remove p tags from images you can do so by adding this snippet to your theme’s (child) functions.php file. or by using the Code Snippets plugin.

// Remove paragraph tag from images
function remove_paratags_from_images($content){
   return preg_replace('​/<p>\s*(<a .*>)?\s*(<img .* \/>)\s*(<\/a>)?\s*<\/p>/iU', '\1\2\3', $content);
}

add_filter('the_content', 'remove_paratags_from_images');

– LAST TESTED DECEMBER 9, 2019

Code not working? Let us know in the comments below.

Latest Snippets

Leave a comment