Clean Up WordPress Shortcode Formatting




Geniuswp show

Summary: TipsLast updated on: January 19, 2018As 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 FunctionSimply 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 ('[' => '[', ']' => ']', ']' => ']');$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[ are replaced with [– Removes opening paragraphs before shortcodesAll instances of]are replaced with ]– Removes closing p tags after shortcodesAll instances of] are replaced with ]– Removes breaks after shortcodesMarch 16, 2022How to Use Lottie Animations in WordPressMarch 14, 2022March 10, 202240+ Best Free Stock Photos SitesSubscribe to the NewsletterGet our latest news, tutorials, guides, tips & deals delivered to your inbox.21 Comments AnthonyThanks, for sharing. I have found that it isn’t work with widget as a shortcode. Any advise?Reply Robb FritzI 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.Reply AJ Clarke | WPExplorerI’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.comReply Gina Stricklind (@gstricklind)The link for ‘Clean WordPress Shortcodes’ does not work..Reply AJ ClarkeThanks for the heads up Gina! I just updated the post instead as I should have in the first place