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:
- Add a shortcode like below:
function callback( $atts, $content = null ) {
return $content;
}
add_shortcode( 'shortcode', 'callback' );
- Insert that ShortCode in text mode to a post or page, like so:
<div>[shortcode]<li>item</li>[/shortcode]</div>
- 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)