Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

The Codex has an example of using get_shortcode_regex() to check if a shortcode is being called on a given page:

$pattern = get_shortcode_regex();
preg_match('/'.$pattern.'/s', $posts[0]->post_content, $matches);
if (is_array($matches) && $matches[2] == 'YOURSHORTCODE') {
    //shortcode is being used
}

This only detects the first shortcode in the post content, though. Is that a bug with get_shortcode_regex(), or can the preg_match() parameters be tweaks to make it return all shortcodes present on the page?

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

From PHP docs (emphasis mine):

preg_match() returns the number of times pattern matches. That will be either 0 times (no match) or 1 time because preg_match() will stop searching after the first match. preg_match_all() on the contrary will continue until it reaches the end of subject.

share|improve this answer
    
That worked, thanks :) –  Ian Dunn Aug 15 '11 at 17:30
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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