Skip to main content
WordPress made easy with the drag & drop Total WordPress Theme - now 40% OFF!Learn More

Clean Up WordPress Shortcode Formatting

Last updated on:

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
Subscribe to the Newsletter

Get our latest news, tutorials, guides, tips & deals delivered to your inbox.

21 Comments

  1. Anthony

    Thanks, for sharing. I have found that it isn’t work with widget as a shortcode. Any advise?

  2. Robb Fritz

    I think this relates to the other issue I wrote you about, which I thought was a theme conflict. Where would this code go (or the correct code on your other post)? I’m afraid I have a feeble understanding of shortcodes.

    • AJ Clarke | WPExplorer

      I’ve updated the theme, if you download the latest version and replace your file at functions/shortcodes.php it should fix the issue. Or you can open a ticket at WPThemehelp.com

  3. Gina Stricklind (@gstricklind)

    The link for ‘Clean WordPress Shortcodes’ does not work..

    • AJ Clarke

      Thanks for the heads up Gina! I just updated the post instead as I should have in the first place 🙂

  4. Yiorgos Theo

    This is an old post but I found it through a google search. It works fine except when styling has been applied to the paragraph, like: [shortcode]
    Here’s my mod:

    function gp_fix_shortcodes($content){   
    	$array = array (
    		'[' => '[', 
    		']' => ']', 
    		']' => ']'
    	);
    	$patternp = '/]*>\[/'; 
    	$content = strtr($content, $array);
    	$content = preg_replace($patternp, "[", $content);
    	return $content;
    }
    add_filter('the_content', 'gp_fix_shortcodes');
    • AJ Clarke

      Great, thanks for sharing

  5. Bijay Pakhrin

    Awesome, Worked perfectly, thanks for sharing 🙂

  6. saraRowGeckocode

    I don’t know why, this solution didn’t work for me. Maybe it’s a bit out of date.
    So I paste here the only solution that worked for me, if anybody needs it:
    http://wordpress.stackexchange.com/questions/46317/stop-auto-formatting-in-shortcodes/46319#46319?s=a2cefc56-7d21-4a24-9b95-850a3b8c2e5d

    Basicaly you reorder filters execution, so wpautop only run after shortcodes are rendered. The only thing you have to do is paste this 2 lines in functions.php or in your shortcodes php file:

    
    remove_filter( 'the_content', 'wpautop' );
    add_filter( 'the_content', 'wpautop' , 12);
    
    • AJ Clarke

      My method should still work, if it’s not working for you something else is going on or maybe you have multiple threaded shortcodes. Removing the wpautop is _doing_it_wrong and can have a lot of side affects, most common is breaking other plugins that output raw html (such as contact forms). I would really recommend against it and you find a good alternative.

  7. premiumwd

    Thanks a bunch, only method I found that worked. Tried multiple instances to clean shortcodes before output.

  8. Jessica

    Hi this works great, except what if you are using shortcodes in a custom field? It only fixes it for the content. Any idea how to apply this to any custom field using a WYSIWYG editor?

    • AJ Clarke

      It’s quite simple Jessica, just pass your meta data through the_content filter…

      apply_filters( 'the_content', $your_meta );
      
  9. Jessica

    I wanted to follow up and say that I found a solution, at least when using the Advanced Custom Fields plugin.

    add_filter('acf_the_content', 'wpex_fix_shortcodes');

    To apply the function to WYSIWYG editors generated by the Advanced Custom Fields plugin.

    • AJ Clarke

      Yep, that would work as well 😉

  10. axisthemes

    AJ Clarke, is there any solution for multiple threaded shortcodes. This works fine if shortcodes aren’t threaded.

    Additionally, update with “\n[” => ‘[‘, too 😛

    • AJ Clarke Avatar AJ Clarke

      Hum, not really sure but if you find a good solution please share, thanks!

  11. dan

    thanks for the acf contribution Jessica

  12. And Finally

    I get a problem with this solution when there’s a shortcode inside a legitimate paragraph and the shortcode is immediately followed by normal paragraph text, like:

    [shortcode] Bla bla bla.

    The solution strips out the opening tag. This causes HTML syntax errors which mean the paragraph disappears in Facebook Instant Articles.

    It looks like the fix is only to strip out p tags when they wrap a shortcode without any intervening text, so your filter would be:

    
    if ( ! function_exists( 'wpex_fix_shortcodes' ) ) {
    	function wpex_fix_shortcodes( $content ) {
    		$content = preg_replace( '|(\[.*\])|', '$1', $content );
    		$content = strtr( $content, ']', ']' );
    
    		return $content;
    	}
    
    	add_filter( 'the_content', 'wpex_fix_shortcodes' );
    }
    
    • AJ Clarke Avatar AJ Clarke

      That’s a good point, how efficient is using preg_replace for this?

  13. Mohamed Abd ElHalim

    Awesome, thanks for sharing 🙂 I want to share a free plugin Shortcode Cleaner Lite, which it clean up unused, broken shortcodes from WordPress content automatically. I hope it will be useful.

Leave a Reply

Your email address will not be published. Required fields are marked *

Learn how your comment data is processed by viewing our privacy policy here.