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.
Good
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
[…] found this useful snippet on wpsnipp.com, this will remove paragraph tags from around images in the_content() on wordpress. Just paste […]
This is brilliant – thank you very much for posting!
No problem anytime j.h.