Tips
Clean Up WordPress Shortcode Formatting
Last Updated on January 11, 2023 · 21 Comments on 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
Thanks, for sharing. I have found that it isn’t work with widget as a shortcode. Any advise?
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.
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
The link for ‘Clean WordPress Shortcodes’ does not work..
Thanks for the heads up Gina! I just updated the post instead as I should have in the first place 🙂
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:
Great, thanks for sharing
Awesome, Worked perfectly, thanks for sharing 🙂
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:
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.
Thanks a bunch, only method I found that worked. Tried multiple instances to clean shortcodes before output.
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?
It’s quite simple Jessica, just pass your meta data through the_content filter…
I wanted to follow up and say that I found a solution, at least when using the Advanced Custom Fields plugin.
To apply the function to WYSIWYG editors generated by the Advanced Custom Fields plugin.
Yep, that would work as well 😉
AJ Clarke, is there any solution for multiple threaded shortcodes. This works fine if shortcodes aren’t threaded.
Additionally, update with “\n[” => ‘[‘, too 😛
Hum, not really sure but if you find a good solution please share, thanks!
thanks for the acf contribution Jessica
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:
That’s a good point, how efficient is using preg_replace for this?
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.