How to remove extra p-tags from shortcode in WordPress? Wpautop function

Written by

When you perform “add_shotcode” in your functions.php, WordPress automatically and unnecessarily adds extra <p> tags above and below the shortcode content. In most of the cases, you don’t need this strange feature. To stop WordPress from adding these empty p-tags you can use this snippet:

remove_filter('the_content', 'wpautop'); 
add_filter('the_content', 'wpautop', 12); 

How does it work?

When a shortcode is executed, its content is passed first through the autoformatting with “wpautop” function. This function replaces line breaks, made in WordPress visual-editor, to <p> and <br> tags. To prevent WordPress from doing this we change the priority of “wpautop” function.

What if we want to use “wpautop” inside our shortcode? Then you should add “wpautop” inside your shortcode function like this:

function your_custom_func($content){
	// adding wpautop formating abilities after changing it's priority
	return $content;
add_shortcode('your_shortcode', 'your_custom_func');