WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
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

Your Answer


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.