Using WordPress ‘get_shortcode_regex()’ PHP function

The get_shortcode_regex() WordPress PHP function retrieves the regular expression used for searching shortcodes in a content. This function combines the shortcode tags into a regex class, allowing to parse and identify different components of a shortcode.

Usage

To use the get_shortcode_regex() function, you simply call it and store its result into a variable. It can be used to find all registered shortcodes or specific ones if they’re passed as an argument. Here’s an example:

$shortcodePattern = get_shortcode_regex(['your_shortcode']);

This line will return the regex pattern to find ‘your_shortcode’ in a string.

Parameters

  • $tagnames (array – Optional): This parameter is a list of shortcodes that you want to find. If not specified, the function will default to all registered shortcodes.

More information

See WordPress Developer Resources: get_shortcode_regex()

This function is part of WordPress core and has been around for a long time, it’s not expected to be depreciated soon. It’s related to other shortcode handling functions like add_shortcode and do_shortcode.

Examples

Detecting a shortcode in global $post

This function checks if a specific shortcode is present in the global $post content:

function detect_my_shortcode() {
  global $post;
  $pattern = get_shortcode_regex(['my_shortcode']);
  if (preg_match_all('/'. $pattern .'/s', $post->post_content, $matches)
    && array_key_exists(2, $matches)
    && in_array('my_shortcode', $matches[2])) {
    // do something if shortcode is being used
  }
}
add_action('wp', 'detect_my_shortcode');

This function can only be used when $post is available. The earliest action you can hook into to get access to it is wp.

Searching for a shortcode in all posts on the index page

This function checks if a specific shortcode is present in any of the posts on the index page:

function detect_my_shortcode() {
  global $wp_query;
  $posts = $wp_query->posts;
  $pattern = get_shortcode_regex(['my_shortcode']);
  foreach ($posts as $post) {
    if (preg_match_all('/'. $pattern .'/s', $post->post_content, $matches)
      && array_key_exists(2, $matches)
      && in_array('my_shortcode', $matches[2])) {
      // do something if shortcode is being used
      break;
    }
  }
}
add_action('wp', 'detect_my_shortcode');

Checking multiple shortcodes

You can pass an array of shortcodes to check if any of them are being used in the content:

function detect_my_shortcodes() {
  global $post;
  $pattern = get_shortcode_regex(['shortcode1', 'shortcode2']);
  if (preg_match_all('/'. $pattern .'/s', $post->post_content, $matches)
    && array_key_exists(2, $matches)
    && array_intersect(['shortcode1', 'shortcode2'], $matches[2])) {
    // do something if any of the shortcodes are being used
  }
}
add_action('wp', 'detect_my_shortcodes');

Escaping shortcodes

The regex also allows for escaping shortcodes with double [[ ]]. If you want to use a shortcode as plain text in your content, you can do it by doubling the square brackets:

$content = 'This is a [[my_shortcode]]';

In this example, my_shortcode will be treated