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?
From: http://codex.wordpress.org/Shortcode_API#Limitations
It will not let you nest the same shortcode inside another though:
Assuming you won’t be doing so, here is a contrived example of what you need to do in your shortcode callback:
Hope this helps.
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.
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.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,
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.