Hi, my name is
Lee Harris and
I'm a Freelance Web Designer

Remove empty p tags from custom shortcodes in WordPress

24.Jun.2017 | ,

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

, ,

  • html5
  • css3
  • sass
  • php
  • WordPress
  • WooCommerce