get_shortcode_regex

Retrieve the shortcode regular expression for searching.

Description

(string) get_shortcode_regex( (null) $tagnames = null ); 

The regular expression combines the shortcode tags in the regular expression in a regex class.

The regular expression contains 6 different sub matches to help with parsing.

1 - An extra [ to allow for escaping shortcodes with double [[]] 2 - The shortcode name 3 - The shortcode argument list 4 - The self closing / 5 - The content of a shortcode when it wraps some content. 6 - An extra ] to allow for escaping shortcodes with double [[]]

Returns (string)

The shortcode search regular expression

Parameters (1)

0. $tagnames — Optional. (null) => null
List of shortcodes to find. Defaults to all registered shortcodes.

Usage

  1. if ( !function_exists( 'get_shortcode_regex' ) ) { 
  2. require_once ABSPATH . WPINC . '/shortcodes.php'; 
  3.  
  4. // Optional. List of shortcodes to find. Defaults to all registered shortcodes. 
  5. $tagnames = null; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = get_shortcode_regex($tagnames); 
  9.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/shortcodes.php  
  1. function get_shortcode_regex( $tagnames = null ) { 
  2. global $shortcode_tags; 
  3.  
  4. if ( empty( $tagnames ) ) { 
  5. $tagnames = array_keys( $shortcode_tags ); 
  6. $tagregexp = join( '|', array_map('preg_quote', $tagnames) ); 
  7.  
  8. // WARNING! Do not change this regex without changing do_shortcode_tag() and strip_shortcode_tag() 
  9. // Also, see shortcode_unautop() and shortcode.js. 
  10. return 
  11. '\\[' // Opening bracket 
  12. . '(\\[?)' // 1: Optional second opening bracket for escaping shortcodes: [[tag]] 
  13. . "($tagregexp)" // 2: Shortcode name 
  14. . '(?![\\w-])' // Not followed by word character or hyphen 
  15. . '(' // 3: Unroll the loop: Inside the opening shortcode tag 
  16. . '[^\\]\\/]*' // Not a closing bracket or forward slash 
  17. . '(?:' 
  18. . '\\/(?!\\])' // A forward slash not followed by a closing bracket 
  19. . '[^\\]\\/]*' // Not a closing bracket or forward slash 
  20. . ')*?' 
  21. . ')' 
  22. . '(?:' 
  23. . '(\\/)' // 4: Self closing tag ... 
  24. . '\\]' // ... and closing bracket 
  25. . '|' 
  26. . '\\]' // Closing bracket 
  27. . '(?:' 
  28. . '(' // 5: Unroll the loop: Optionally, anything between the opening and closing shortcode tags 
  29. . '[^\\[]*+' // Not an opening bracket 
  30. . '(?:' 
  31. . '\\[(?!\\/\\2\\])' // An opening bracket not followed by the closing shortcode tag 
  32. . '[^\\[]*+' // Not an opening bracket 
  33. . ')*+' 
  34. . ')' 
  35. . '\\[\\/\\2\\]' // Closing shortcode tag 
  36. . ')?' 
  37. . ')' 
  38. . '(\\]?)'; // 6: Optional second closing brocket for escaping shortcodes: [[tag]]