4

If you are familiar with this code

<?php
  $pattern = get_shortcode_regex();
  preg_match('/'.$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?

2
  • 1
    Can you update your question with an example text containing the type of shortcode you are looking for? I'm not really sure what you want to match when you talk about the nested shortcode: all occurrences of the shortcode, or only the shortcode if it's in the content of another shortcode?
    – Jan Fabry
    Nov 30, 2010 at 11:45
  • If you're doing the shortcode parsing on your own, is a bit like asking for trouble. Not that the wordpress parser is perfect, but that done you need to take care on your own. Instead register your shortcode via the API: codex.wordpress.org/Shortcode_API - should make your life easier in the end.
    – hakre
    Nov 30, 2010 at 14:52

3 Answers 3

8

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.

3

Just a guess. get_shortcode_regex() only works for registered shortcodes. So just in case you have not registered "your" shortcode it does not even work for simple shortcodes. So this might be - in case you did not register it - the cause of your problem in the end.

Shortcode handling is specified in the Shortcode API, examples how to register shortcodes are given on the add_shortcode() Codex page as well.

function baztag_func($atts, $content=) {
    return "content = $content";
}
add_shortcode('baztag', 'baztag_func');

For nested codes, Jeff has already answered how this works. So this is basically a smaller example that shows how to add a shortcode. After using add_shortcode(), get_shortcode_regex() will return a regular expression that actually covers the registered shortcode name. Otherwise it just won't match.

1

Shortcode matching is implemented in a deficient way (shortcodes.php, ll174). Basically, regular expressions are used to describe what should be a non-regular language (i.e. shortcodes with arbitrary nesting).

In effect,

[a]
  [a]
  [/a]
[/a]

will be parsed so that the opening tag in line 1 matches the closing tag in line 3; obviously, that does not lead to the desired behaviour.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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