How to Parse Nested Shortcode with the Same Name

How to Parse Nested Shortcode with the Same Name

Parsing shortcodes such as this one

[row]
    [col size="6"]...[/col]
    [col size="6"] // second col
        [row]
            [col size="6"]...[/col] // second col end, wrong
            [col size="6"]...[/col]
        [/row]
    [/col]
[/row]

can be tricky because the second [col] is ended by the one nested in the second, nested [row]

so I have found that the best solution is to parse the second [row] first , before the first [row]

just add a the_content filter

add_filter( "the_content", 'parse_content_for_nested_shortcodes' ); 

and this is the function

function parse_content_for_nested_shortcodes($content){
        
        $matches=array();
          $subject = $content;
$pattern = '/[row.*?([row.*?].*?[/row])/';
preg_match_all($pattern, $subject, $matches);
if(count($matches)>0 && isset($matches[1])){
    foreach($matches[1] as $regmatch){
        $content = str_replace($regmatch, stripslashes(do_shortcode($regmatch)), $content);
    }
}


        return $content;
    }

  • tom
    August 2, 2014 at 12:12 pm

    hello, i’ve bought dzscalendar plugin. I have a problem how to use calendar in header (shortcode not work) ?
    i tried to use :

  • bluebusinessdialog
    March 19, 2015 at 10:30 pm

    I¡¦m not positive where you are getting your information, however good topic. I needs to spend some time learning much more or working out more. Thanks for excellent info I used to be searching for this information for my mission.

  • freedomteamautogroup
    June 3, 2015 at 6:54 am

    Hey very nice web site!! Man .. Excellent .. Amazing .. its a really great blog post of i enjoyed lot.thanks again for share with us.

  • boisefoodstorage
    June 3, 2015 at 6:59 pm

    I simply want to mention I’m beginner to blogging and really liked this website. Very likely I’m likely to bookmark your site . You surely come with tremendous well written articles. Cheers for sharing your blog site.

  • ny-businesslaw
    June 7, 2015 at 11:49 am

    Your mode of telling all in this paragraph is genuinely pleasant, all be capable of without difficulty understand it, Thanks a lot.

Leave a comment

Your email address will not be published

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

43 − = 40