Resolving wpautop and Shortcode problems

Background

WordPress offers quite a friendly hybrid WYSIWYG / HTML editor that makes publishing easy even for those with no HTML experience. One of the ways it accomplishes this is through a function called wpautop. This runs on the_content as the page is drawn to insert <p> and <br/> tags as needed. By inserting them dynamically like this it prevents code creeping in to the editor and leading to potential confusion / corruption. However, it can sometimes lead to unexpected results – especially when combined with shortcodes.

The problem with wpautop and Shortcodes

While WordPress does try to detect shortcodes and prevent unexpected behaviours, I have found it still occurs and often ends up with the shortcode itself being wrapped in <p> tags – this isn’t usually a major problem, but creates messy code which just bugs me. Also, quite a lot of plugins, and some badly designed themes apparently, will trigger wpautop inside shortcodes. Recently I found that one of my favourite plugins – Simple Fields – does this on it’s textareas which was causing a shortcode inserting their content to be prepended with a <br/>. In this case it was a particular problem as the shortcode in question was generating columns and the unexpected tag was breaking the layout.

Solution

It is possible to completely turn off wpautop by adding the following line to your function.php file (or using one of many plugins):

remove_filter( 'the_content', 'wpautop' );

This works great is all your editors are tech savvy and happy writing markup by hand, but otherwise it is taking a sledgehammer to crack a nut and will likely just lead to unformatted text. Even if you use the visual editor, no tags are inserted automatically when you hit Return.

However we are half way there. The root cause isn’t that wpautop is running, but that it is being run on the_content before do_shortcode.

Luckily WordPress provides a handy way to set the priority of functions. All we have to do is remove wpautop, then re-add it and define the priority as something greater than the default (10) that is also greater than the priority for do_shortcode, which is 11.

Adding the following code to your functions.php will make sure that it runs after everything else, keeping the usability but stopping the random insertions:

remove_filter( 'the_content', 'wpautop' );
add_filter( 'the_content', 'wpautop' , 99);

You may get unexpected behaviours depending on what other plugins are doing / modifications have been made so you may have to tweak the priority to get everything running smoothly.

Category:

Tags:

11 responses to “Resolving wpautop and Shortcode problems”

  1. Konrad avatar

    works!
    thank you!

  2. Teddi Deppner avatar

    Beautiful! Why isn’t this in WordPress.org’s documentation? Your solution works far better than the 20 other pages I read while researching this issue.

    Thank you much for sharing it!

  3. Tim avatar

    Thanks. Your solution makes total sense! All the others seem to be a half solution, and you provide the full solution!

    Thank you

  4. Kay avatar
    Kay

    You are a genius. Thank you loads for sharing this, the most useful WP tip in ages.

  5. Drew avatar

    Wonderful !

    I was worried I’d have to clone/recode plugins to get my site compliant, but this is a far more elegant solution.
    Thank you, Phil.

  6. anachnus avatar
    anachnus

    thank you!! 😀

  7. Chiedo avatar

    All the solutions I’ve seen so far either complete remove autop functionality from all shortcodes or remove the ability to intentionally add br and p tags between the shortcodes.

    The key is to recreate the wpautop function with the ability to specify shortcodes that it should not run the filter on.

    I don’t mean to self promote but I’m not sure of another way to share the code as it is quite long.

    I did so here:
    https://github.com/chiedolabs/shortcode-wpautop-control

  8. Chris avatar
    Chris

    Dude you rule… I was scratching my head on this for an hour before I came across your post which totally saved the day.

  9. Paolo avatar
    Paolo

    Thank you. This was VERY helpful.

  10. AJ Michels avatar
    AJ Michels

    We have tried this but now all of the HTML that our Shortcodes generate is being impacted by wpautop. For example. I might have a shortcode that outputs something like

    and I end up with something like this

    Any thoughts? I have been looking all over and finding a lot of people posting solutions that solve one problem but just introduce another.

    1. Phil Banks avatar

      Hi,

      Sorry – your code got stripped during sanitization, feel free to drop it in a gist if you want to share it.

      Unfortunately this can be hit or miss and there are so many variables including what your theme or other plugins are doing I have found it is always a try it and see problem…
      You could try varying the priority (99) to see if moving it a bit earlier or later helps. Or have a dig through the shortcode code (if they arn’t your own of course). Or lastly try the old faithful of disabling all plugins and using a WP theme (e.g.: TwentySeventeen) and testing it there as it could be a problem introduced by another plugin or theme.

      All the best,
      Phil

Leave a Reply

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