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

php - Split shortcodes to array of shortcodes

programmeradmin1浏览0评论

For example, I have a very complex shortcodes like this:

[shortcode-1 attr1 attr2]
    [child-shortcode-1 attr1="231czx"]
        ...
    [/child-shortcode-1]
    [child-shortcode-2 /]
[/shortcode-1]
[shortcode-2 attr1="cxzc"]
    ...
[/shortcode-2]
[shortcode-3 attr1="123" attr2="456" /]

How can I split these shortcodes to:

array(
    0 => '[shortcode-1 attr1 attr2]
    [child-shortcode-1 attr1="231czx"]
        ...
    [/child-shortcode-1]
    [child-shortcode-2 /]
[/shortcode-1]',
    1 => '[shortcode-2 attr1="cxzc"]
    ...
[/shortcode-2]',
    3 => '[shortcode-3 attr1="123" attr2="456" /]'
);

Thank you!

For example, I have a very complex shortcodes like this:

[shortcode-1 attr1 attr2]
    [child-shortcode-1 attr1="231czx"]
        ...
    [/child-shortcode-1]
    [child-shortcode-2 /]
[/shortcode-1]
[shortcode-2 attr1="cxzc"]
    ...
[/shortcode-2]
[shortcode-3 attr1="123" attr2="456" /]

How can I split these shortcodes to:

array(
    0 => '[shortcode-1 attr1 attr2]
    [child-shortcode-1 attr1="231czx"]
        ...
    [/child-shortcode-1]
    [child-shortcode-2 /]
[/shortcode-1]',
    1 => '[shortcode-2 attr1="cxzc"]
    ...
[/shortcode-2]',
    3 => '[shortcode-3 attr1="123" attr2="456" /]'
);

Thank you!

Share Improve this question asked Jan 13, 2017 at 4:40 Vĩnh Nguyễn TrọngVĩnh Nguyễn Trọng 1 2
  • It appears you have answered your own question. – ngearing Commented Jan 13, 2017 at 5:37
  • No. I want the string to contain shortcodes to be split into an array of shortcodes. – Vĩnh Nguyễn Trọng Commented Jan 13, 2017 at 6:49
Add a comment  | 

1 Answer 1

Reset to default 1

Wordpress already has built in support for Nested Shortcodesprovided their handler functions support it by recursively calling.

So if you have for example:

[tag-a]
   [tag-b]
      [tag-c]
   [/tag-b]
[/tag-a]

As long as you recursively call do_shortcode in your shortcode function, all the shortcodes would be parsed.

So you would define the functions for your shortcodes as such:

For tag-a:

function tag_a( $atts ){
    $output = "";
    return do_shortcode($output);
}
add_shortcode( 'tag-a', 'tag_a' );

For tag-b:

function tag_b( $atts ){
    $output = "";
    return do_shortcode($output);
}
add_shortcode( 'tag-b', 'tag_b' );

For tag-c:

function tag_c( $atts ){
    $output = "";
    return do_shortcode($output);
}
add_shortcode( 'tag-c', 'tag_c' );
发布评论

评论列表(0)

  1. 暂无评论