I just discovered the beauty of working with WordPress shortcodes and the the frustration that can come along with it. If you’re building websites for clients, it can mean the difference between repeat calls about them breaking code in the backend and the peace and quite of a satisfied client who knows how to edit their content.
If you’re working with shortcodes, you’ve no doubt come across phantom <br> or <p> tags. WordPress automatically adds these in with theย wpautop() function. These tags can effectively break your carefully coded content and cause you hours of frustration, not to mention calls from your clients.
Fortunately for you there’s an easy fix to the problem. We could just do away with the wpautotop() function, but that would cause a lot of other issues within the visual editor, a big problem when working with client sites. However with a simple php function we can eradicate those return and paragraph tags in your carefully coded shortcode content. In your theme’s functions.php file before your shortcodes, insert the following code:
[codebox 1]
And then add this line into your shortcode function or functions:
[codebox 2]
Voila! You’re done. Now go test your now finally working shortcode.
Thanks goes toย Donal MacArthur for this incredibly useful bit of information.
Users, hereโs what theme developers are not telling you: By using their themeโs shortcodes, they are essentially locking you into using their themes forever.
Only if you don’t know how to edit your own code and functionality!
Not quite true. That might be the intention of some developers, but responsible theme developers try to provide a plugin that retains the codebase for any shortcodes they use. They are not, however, held responsible for future-proofing your site by providing stylesheets that are cross-compatible with other themes. That’s the difference between plug-and-play and custom development.
The problem with your argument is that you’re expecting themes that cost in the neighborhood of $40-70 to keep working for you when you decide you no longer wish to use that developer’s codebase. Chances are that yes- A developer’s collection of themes will all be cross-compatible since they reuse source code for compatibility, but this isn’t an attempt to “lock” you in. It’s just an attempt to keep their code clean, concise, and cross-compatible.
If you don’t like built-in shortcodes, their are dozens of quality plugins that will keep working for you down the road- Just don’t be surprised if they don’t quite fit perfectly with the appearance of the theme you’re using. Shortcodes are an “add-on” and should be used with a certain amount of caution and planning depending on the size and scalability you expect from your site.
YES! I’ve been banging my head against the wall with this one for WAY too long. I’ve made numerous attempts to resolve but nothing I’ve found has worked… until now. Thanks so much for posting this. You’re a lifesaver.
(And this page definitely needs a higher rank in Google’s search results…)
Cheers!
You’re welcome!!
And I agree about Google’s search results ๐
Thanks for this writeup. I just put it to use in a theme and it works perfectly so far.
One suggestion: the code styler you’re using here makes it nearly impossible for me to read the code in the post. The dark keyword colors agains the dark background is painful!
Thanks again for sharing your code.
Thanks Morgan! Youโre right about the colors, I definitely need to change that.
Brilliant!! Works on nested shortcodes too…. Thanks very much! ๐
Hi. I’m using a plugin that requires a shortcode that pulls in some particular content (it’s just plain text content). However it line breaks around the shortcode content. I want to include it within a sentence.
In reference to this part of your post, where can I include this since I’m using a plugin and not hardcoding the shortcodes myself?:
And then add this line into your shortcode function or functions:
$content = parse_shortcode_content( $content );
Thanks.