amitabhaghosh197
9/7/2016 - 10:09 AM

WordPress - Remove empty p tags for custom shortcodes

WordPress - Remove empty p tags for custom shortcodes

// Remove all P tags from shortcodes
<?php

add_filter("the_content", "the_content_filter");

function the_content_filter($content) {

	// array of custom shortcodes requiring the fix 
	$block = join("|",array("col","shortcode2","shortcode3"));

	// opening tag
	$rep = preg_replace("/(<p>)?\[($block)(\s[^\]]+)?\](<\/p>|<br \/>)?/","[$2$3]",$content);
		
	// closing tag
	$rep = preg_replace("/(<p>)?\[\/($block)](<\/p>|<br \/>)?/","[/$2]",$rep);

	return $rep;

}