X

Remove p Tag From Around Images in the_content

Snippets by IsItWP

Do you want to remove the paragraph tags from around the images? By default, when you insert images in your content, it’ll create a paragraph tag around the image.

Instructions:

All you have to do is add this code to your theme’s functions.php file or in a site-specific plugin:

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

Note: If this is your first time adding code snippets in WordPress, then please refer to our guide on how to properly copy / paste code snippets in WordPress, so you don’t accidentally break your site.

If you liked this code snippet, please consider checking out our other articles on the site like: 9 best WordPress accordion plugins and 50 best WooCommerce themes for your online store.

Comments   Leave a Reply

  1. Good

  2. Better solution and faster (already included in WordPress).

    By default, WordPress adds paragraph “ tags to category descriptions. Stop this by adding the following to your **functions.php** file

    // Remove p tags from category description
    remove_filter(‘term_description’,’wpautop’);

    **Simple and easy (codeless).**

    Thank you

  3. 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 […]

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

    1. No problem anytime j.h.

Add a Comment Cancel reply

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!