WordPress hook: Automatically remove p tags on images

Simply paste the following code on your theme functions.php file, save it, and the <p> tags are gone.

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 James for the great hack!

Looking for WordPress hosting? Try WP Web Host. Prices starts at $5/month and you can try it for free!

WordPress hook: Automatically remove p tags on images

Leave a Reply

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