This is a shortcode example.
[a][b]something[/b][/a]
It should be used like this with do_shortcode($content);
function a_shortcode($atts = [], $content = null) {
do_shortcode($content);
return $result;
}
add_shortcode('a', 'a_shortcode');
function b_shortcode($atts = [], $content = null) {
return $whatever;
}
add_shortcode('b', 'b_shortcode');
Is there a reason you dont use content without do_shortcode?
[a][b]something[/b][/a]
function a_shortcode($atts = [], $content = null) {
//use content here, parse [b]something[/b] as string and do whatever
return $result;
}
This is a shortcode example.
[a][b]something[/b][/a]
It should be used like this with do_shortcode($content);
function a_shortcode($atts = [], $content = null) {
do_shortcode($content);
return $result;
}
add_shortcode('a', 'a_shortcode');
function b_shortcode($atts = [], $content = null) {
return $whatever;
}
add_shortcode('b', 'b_shortcode');
Is there a reason you dont use content without do_shortcode?
[a][b]something[/b][/a]
function a_shortcode($atts = [], $content = null) {
//use content here, parse [b]something[/b] as string and do whatever
return $result;
}
Share
Improve this question
edited Dec 4, 2019 at 6:49
Krzysiek Dróżdż
25.6k9 gold badges53 silver badges74 bronze badges
asked Dec 4, 2019 at 5:02
ToniqToniq
4476 silver badges15 bronze badges
1 Answer
Reset to default 0You don’t have to use do_shortcode
inside your shortcode callback function.
But if you don’t do this, then you won’t be able to nest shortcodes, so other shortcodes added on the site won’t work inside it.
Let’s say your shortcode callback looks like this:
function a_shortcode($atts = [], $content = null) {
//use content here, parse [b]something[/b] as string and do whatever
$result = '<div>'. $content .'</div>';
return $result;
}
So it takes given content and wraps it in div. Because it doesn’t run do_shortcode
, all shortcodes placed in content will be ignored and they won’t be parsed.
Sometimes it’s OK - if you write a shortcode to display a button, you (most probably) don’t won’t to be able to place an advanced shortcode as label of that button.
Sometimes it’s not OK - if you write a shortcode for columns, you want to be able to place other shortcodes (like button) inside that column...