If you are familiar with this code

<?php
  $pattern = get_shortcode_regex();
  preg_match("https://wordpress.stackexchange.com/".$pattern.'/s', $posts[0]->post_content, $matches);
  if (is_array($matches) && $matches[2] == 'YOURSHORTCODE') {
    //shortcode is being used
  }
?>

from this website, but it does not work for nested shortcodes.

Does anyone know how to make it work for nested shortcodes?

3 Answers
3

From: http://codex.wordpress.org/Shortcode_API#Limitations

The shortcode parser correctly deals with nested shortcode macros, provided their handler functions support it by recursively calling do_shortcode()

It will not let you nest the same shortcode inside another though:

However the parser will fail if a shortcode macro is used to enclose another macro of the same name

Assuming you won’t be doing so, here is a contrived example of what you need to do in your shortcode callback:

function paragraph_wrap($atts, $content) {

     // if there are nested shortcodes - handle them
     $content = do_shortcode($content);

     // wrap content in a paragraph tag
     $paragraphed = '<p>' . $content . '</p>';

     return $paragraphed;
}
add_shortcode('wrap_p', 'paragraph_wrap');

Hope this helps.

Leave a Reply

Your email address will not be published. Required fields are marked *