Search Blog

A quick way to remove empty p tags from custom shortcodes in WordPress.

<?php

add_filter("the_content", "the_content_filter");

function the_content_filter($content) {

	$block = join("|",array("shortcode1","shortcode2","shortcode3"));

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

	return $rep;

}

Credit: https://gist.github.com/bitfade