fbpx

Get the Official WPSaviour App Now!

Tips

Clean Up WordPress Shortcode Formatting

As I was working on a new premium WordPress theme for Themeforest I was having issues with my Pricing Table shortcode as it kept adding extra spacing due to stray empty paragraph (p) tags that were being added automatically by WordPress. Doing a little searching I found a great solution on the TF forum.

Clean Up WordPress Shortcodes Function

Simply copy and paste the following code into your functions.php file or wherever your hold your shortcodes. This function will clean up the output of your shortcodes, which is especially important for nested shortcodes.

if( !function_exists('wpex_fix_shortcodes') ) {
	function wpex_fix_shortcodes($content){   
		$array = array (
			'<p>[' => '[', 
			']</p>' => ']', 
			']<br />' => ']'
		);
		$content = strtr($content, $array);
		return $content;
	}
	add_filter('the_content', 'wpex_fix_shortcodes');
}

What it does…This piece basically grabs all the post content before it’s outputted and replaces specific code as mentioned below:

  • All instances of <p>[ are replaced with [ – Removes opening paragraphs before shortcodes
  • All instances of ]</p> are replaced with – Removes closing p tags after shortcodes
  • All instances of ]<br /> are replaced with – Removes breaks after shortcodes

Download The WPSaviour App Now

Related posts
Tips

How To Use Pop-ups Without Harming SEO

Tips

10 WordPress Resources You Need to Bookmark Right Now

Tips

An Intro to the Anatomy of a WordPress Theme

Tips

Redesigning Your WordPress Website To Add a Personal Touch (The Sequel)

Sign up for our Newsletter and
stay informed

Leave a Reply

Your email address will not be published.