Add a Feature to core CP add_shortcode to deal with unwanted wpautop

Enclosing shortcodes usually avoid adding wpautop by default but for example when you add HTML like <li></li> inside the enclosing shortcode, then wpautop kicks in and produces unwanted results.

The proposal is to add a 3rd argument to add_shortcode() which allows to opt out of wpautop completely.

Step by step:

  1. Add a shortcode like below:
function callback( $atts, $content = null ) {
	return $content;
}
add_shortcode( 'shortcode', 'callback' );
  1. Insert that ShortCode in text mode to a post or page, like so:
    <div>[shortcode]<li>item</li>[/shortcode]</div>
  2. Check the source html produced by this on the front end. It will be:
<div>
<p></p>
<li>item</li>
<p> </p>
</div>

The only solution to remove those unwanted p tags, is removing wpautop globally, or like this plugin does, or removing with priority 9, and re-add with priority 11.

@James objected that the plugin way would add unnecessary overhead.

Thus, the current best solution is this:

remove_filter( 'the_content', 'wpautop', 9);
add_filter( 'the_content', 'wpautop', 11);

We remove the wpautop just before shortcodes are done, and add just after.

The disadvantage of this is, it affects all shortcodes (versus the plugin Solution, which affects only the shortcodes you want)

Thus, there would be an option to add this to core, by adding a third parameter to add_shortcode so we can opt out of wpautop if we want.
add_shortcode( 'tag', 'callback', false ) (would then remove wpautop from our registered shortcode)
add_shortcode( 'tag', 'callback' ) (would act as it does by default)

4 Likes

Sounds a good idea.

3 Likes

There was a good petition for redoing shortcode parser. This could be tied into that.

3 Likes

Yes, Greg got started on that, then got busy on other things …

It’s important for any change to wpautop to have the minimal effects possible on other code and other places in the site, because there are a lot of interactions of this behavior with other plugins, often in surprising places, for example: Add Option To Turn Off wpautop

One way this petition could work that would have the minimal effect in other places would be to add a new “group” of shortcodes that run at a different priority. For example, if wpautop runs today at filter priority 9, and then shortcodes run at filter priority 10, then we could add a new hook to run only the shortcodes with this new $do_wpautop = false parameter set, to run at priority 8 to avoid this issue. (Details to be ironed out and tested but hopefully that makes sense conceptually.)

3 Likes