1. 首页
  2. WordPress 函数手册

get_shortcode_regex()

get_shortcode_regex( array $tagnames = null )

检索要搜索的shortcode正则表达式。
Retrieve the shortcode regular expression for searching.

目录锚点:#说明#参数#返回#源码#笔记


说明(Description)

正则表达式将正则表达式中的shortcode标记组合到regex类中。

正则表达式包含6个不同的子匹配项以帮助解析。

1–一个额外的[允许用double[[]]转义快捷方式2–快捷方式名称3–快捷方式参数列表4–自动关闭/5–快捷方式包装某些内容时的内容。6–一个额外的]允许用double[[]]转义短代码


参数(Parameters)

参数 类型 必填 说明
$tagnames (array) 可选 要查找的快捷方式列表。默认为所有注册的快捷方式。

返回(Return)

(string)shortcode搜索正则表达式


源码(Source)

/**
 * Retrieve the shortcode regular expression for searching.
 *
 * 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 [[]]
 *
 * @since 2.5.0
 *
 * @global array $shortcode_tags
 *
 * @return string The shortcode search regular expression
 */
function get_shortcode_regex() {
	global $shortcode_tags;
	$tagnames = array_keys($shortcode_tags);
	$tagregexp = join( '|', array_map('preg_quote', $tagnames) );

	// WARNING! Do not change this regex without changing do_shortcode_tag() and strip_shortcode_tag()
	// Also, see shortcode_unautop() and shortcode.js.
	return
		  '\['                              // Opening bracket
		. '(\[?)'                           // 1: Optional second opening bracket for escaping shortcodes: [[tag]]
		. "($tagregexp)"                     // 2: Shortcode name
		. '(?![\w-])'                       // Not followed by word character or hyphen
		. '('                                // 3: Unroll the loop: Inside the opening shortcode tag
		.     '[^\]\/]*'                   // Not a closing bracket or forward slash
		.     '(?:'
		.         '\/(?!\])'               // A forward slash not followed by a closing bracket
		.         '[^\]\/]*'               // Not a closing bracket or forward slash
		.     ')*?'
		. ')'
		. '(?:'
		.     '(\/)'                        // 4: Self closing tag ...
		.     '\]'                          // ... and closing bracket
		. '|'
		.     '\]'                          // Closing bracket
		.     '(?:'
		.         '('                        // 5: Unroll the loop: Optionally, anything between the opening and closing shortcode tags
		.             '[^\[]*+'             // Not an opening bracket
		.             '(?:'
		.                 '\[(?!\/\2\])' // An opening bracket not followed by the closing shortcode tag
		.                 '[^\[]*+'         // Not an opening bracket
		.             ')*+'
		.         ')'
		.         '\[\/\2\]'             // Closing shortcode tag
		.     ')?'
		. ')'
		. '(\]?)';                          // 6: Optional second closing brocket for escaping shortcodes: [[tag]]
}
更新版本 源码位置 使用 被使用
4.4.0 wp-includes/shortcodes.php:249 6 0

笔记(Notes)

例子

get_shortcode_regex() 为WP2原创文章,链接:https://www.wp2.cn/functions/get_shortcode_regex/

发表评论

您的电子邮箱地址不会被公开。