php - Split shortcodes to array of shortcodes

For example, I have a very complex shortcodes like this:[shortcode-1 attr1 attr2][child-shortcode-1 attr1="231czx&q

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' );

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745334402a4623036.html

相关推荐

  • php - Split shortcodes to array of shortcodes

    For example, I have a very complex shortcodes like this:[shortcode-1 attr1 attr2][child-shortcode-1 attr1="231czx&q

    3小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信