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?


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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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