返回介绍

get_shortcode_regex()

发布于 2017-09-11 00:09:33 字数 5491 浏览 942 评论 0 收藏 0

get_shortcode_regex( array $tagnames = null )

Retrieve the shortcode regular expression for searching.


description

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


参数

$tagnames

(array) (Optional) List of shortcodes to find. Defaults to all registered shortcodes.

Default value: null


返回值

(string) The shortcode search regular expression


源代码

File: wp-includes/shortcodes.php

function get_shortcode_regex( $tagnames = null ) {
	global $shortcode_tags;

	if ( empty( $tagnames ) ) {
		$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]]
}

更新日志

Versiondescription
4.4.0Added the $tagnames parameter.
2.5.0Introduced.

相关函数

Used By

  • wp-includes/shortcodes.php: do_shortcodes_in_html_tags()
  • wp-admin/includes/ajax-actions.php: wp_ajax_parse_embed()
  • wp-includes/shortcodes.php: has_shortcode()
  • wp-includes/shortcodes.php: do_shortcode()
  • wp-includes/shortcodes.php: strip_shortcodes()
  • wp-includes/media.php: get_post_galleries()
  • Show 1 more used by Hide more used by

User Contributed Notes

  1. Skip to note content You must log in to vote on the helpfulness of this noteVote results for this note: 0You must log in to vote on the helpfulness of this note Contributed by Codex

    Example

    
    /**
     * Detect shortcodes in the global $post.
     */
    function wpdocs_detect_shortcode() {
    	global $post;
    	$pattern = get_shortcode_regex();
    
    	if (   preg_match_all( '/'. $pattern .'/s', $post->post_content, $matches )
    		&& array_key_exists( 2, $matches )
    		&& in_array( 'your-shortcode', $matches[2] )
    	) {
    		// shortcode is being used
    	}
    }
    add_action( 'wp', 'wpdocs_detect_shortcode' );
    

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

    The following code is an adjustment of the first example, but able to also search all posts on the index page.

    
    /**
     * Detect shortcodes in the global $post.
     */
    function wpdocs_detect_shortcode() {
    	global $wp_query;	
    	$posts = $wp_query->posts;
    	$pattern = get_shortcode_regex();
        
    	foreach ( $posts as $post ) {
    		if ( preg_match_all( '/'. $pattern .'/s', $post->post_content, $matches )
    			&& array_key_exists( 2, $matches )
    			&& in_array( 'videoannotation', $matches[2] )
    		) {
    			// enque my css and js
    			break;	
    		}    
    	}
    }
    add_action( 'wp', 'wpdocs_detect_shortcode' );
    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文