Loading...

Blog

How to Remove the Paragraph Tag from Images inside the WordPress Post Content

Images are a crucial piece to the overall look and feel of pretty much every WordPress blog or website. When images are uploaded to the post content, the default of WordPress is to wrap them in paragraph tags. This however adds extra unwanted markup and can also affect and cause problems with the inner styling of your content blocks. In this tutorial we will teach you how to remove the unwanted paragraph tag from images uploaded to the post content by using a simple WordPress function.

Remove the Paragraph Tag from Images via WordPress Functions

In order to remove paragraph tags from wrapping around all images inside the WordPress post content you will need to add a PHP filter to your WordPress Functions file. Simply open your functions.php file then copy and paste the code below. The paragraph tag will now be completely removed from all images uploaded to your WordPress post content.

PHP

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

Troubleshooting

If you are having problems getting this tutorial to work please reread the tutorial and try again, if you still cannot get it to work please leave us a comment below and we will respond as soon as possible. Please do not email us with problems regarding this tutorial, only comments will be responded to.

Leave a Comment

1 comments