Remove unwanted p tags on Wordpress

unwanted p tag wordpress

Here in this short guide you will learn how to remove unwanted p tags on wordpress.

Sometimes Wordpress sites automatically generate unwanted <p> tags within content.

This may create unwanted white spaces which may not good for designing your site or content.

Example Problem:
<p>This is paragraph 1.</p>
<p>This is paragraph 2.</p>
<p></p>
<p>This is paragraph 3.</p>

To remove unwanted white spaces with <p> tags on wordpress just paste the below javascript/jQuery code before the closing of body (</body) tag.

jQuery('p:empty').remove();

Also in wordpress sites sometimes the images get loaded surrounded by <p></p> tags.

Example Problem:

<p><img src=image_file.jpg /></p>

To remove the unwanted p tags within images just paste the below code within your function.php file and save.

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