最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

functions - Using shortcode content with nested shortcode

programmeradmin2浏览0评论

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
Add a comment  | 

1 Answer 1

Reset to default 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...

发布评论

评论列表(0)

  1. 暂无评论