Is this the best way to enable nested shortcodes?:
add_filter( 'the_content', 'do_shortcode' );
I keep finding mixed results and want to be sure I won’t break anything else!
Is this the best way to enable nested shortcodes?:
add_filter( 'the_content', 'do_shortcode' );
I keep finding mixed results and want to be sure I won’t break anything else!
You must be logged in to post a comment.
http://codex.wordpress.org/Shortcode_API#Nested_Shortcodes
Everything you need to know about nested shortcodes in WP
Here’s a specific answer, to avoid relying on the official WP docs in the other answer (which, it seems, don’t really offer the explicit instructions anyway).
Run do_shortcode() on the input
Here is an example registration of a shortcode that supports nested shortcodes inside of it:
You would use this like:
The key ingredient, of course, is running the
$text
content (that comes from between the opening and closing shortcode tag) throughdo_shortcode()
. By default WP grabs the first shortcode it can see, then ignores any shortcodes inside that, so we need to tell it to “look again”, and this code does the trick.Of course, the example above does nothing useful, you’ll need to take action on the
$output
using$atts
etcetera to make it do something.Warnings about nesting shortcodes with closing tags
So here’s my rule of thumb: If a nested shortcode has a closing tag, it must either be first, the only of it’s kind, or all of the matching nested shortcodes need to have closing tags.
So an example like this won’t give you what you expect:
What happens? You get the first shortcode with
id=1
rendered with the[/nested_shortcode]
as its closing tag, and theid=2
shortcode as its unrendered content! Yikes!So like I said above, either avoid having the same shortcode multiple times, or make sure that ALL of them have both the opening and closing shortcodes!