Shortcodes and Line Breaks in WordPress

This post may contain affiliate links. As an Amazon Associate I also earn from qualifying Amazon purchases at no cost to you. Full disclosure here.

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.

All-In-One Business Bookkeeping Spreadsheet

Share:

Facebook
Twitter
Pinterest
LinkedIn

9 thoughts on “Shortcodes and Line Breaks in WordPress”

    1. 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.

  1. 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!

  2. 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.

  3. 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.

Leave a Reply to charles Cancel Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

More Good Reads

Related Posts

person in long sleeve shirt holding a calculator

What is the Easiest Bookkeeping Method for Small Business?

As a small business owner, keeping track of your finances doesn’t have to be complicated. The easiest bookkeeping method for small businesses is using a simple spreadsheet to record income and expenses. This straightforward cash basis method can save you time and headaches compared to traditional accounting. Why Use a Spreadsheet? Spreadsheets are easy to

two women sitting in front of computer monitor

Whale Hunting – The Risk of Having One Major Client

In 2012 we shut down our startup. It was devastating and emotionally draining. Walking away from something you’ve put your blood sweat and tears into is a gut wrenching experience. Fortunately we had a team with a skillset so we pivoted to being a software consulting business. Hooray, we’re saved! Very quickly we signed a

selective focus photography of hustle and bust text

Business Name Brainstorming – How-to Guide

If the idea of finding the right name for your business fills you with anxiety, you’re not alone. Business name brainstorming can be really challenging. Fortunately, there’s a formula that makes it easier. In my years as an entrepreneur, I’ve come up with at least a dozen brand names for companies I’ve started or co-founded,

Shopping Cart
[]