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!

  • It appears you have answered your own question. – ngearing Jan 13 '17 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 Jan 13 '17 at 6:49
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' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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