Remove p tag from around images in the_content

Adding this snippet to the functions.php of your wordpress theme will remove the HTML p tag from around images in the_content.

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

Comments  Leave a Reply

  1. Filter Paragraph Tags From Images In The_Content | Leach Creative May 3, 2012 at 1:28 pm

    […] found this useful snippet on wpsnipp.com, this will remove paragraph tags from around images in the_content() on wordpress.  Just paste […]

  2. This is brilliant – thank you very much for posting!

    1. No problem anytime j.h.

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!