1

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;

}

1 Answer 1

0

You 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...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.