^ 回到顶部
  • 人生没有定律,每个人都有自己的节奏
  • 本站wordpress建站教程均通过实践后发布,希望对你有帮助
  • 希望你的坚持,都是因为热爱,而不是因为不甘心
  • 8年wordpress建站经验,5星服务品质
  • 那些不愿意让你吃亏的人,才是真正值得你深交的人,也是值得你付出时间的人
  • 阿里云2核2g仅需86元/年

wordpress函数get_shortcode_regex()用法示例

get_shortcode_regex( array $tagnames = null )

Retrieve the shortcode regular expression for searching.


描述

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

新用户2核2g仅需86元/年    领取1153套模板    免费cdn流量包    ¥3188礼包

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

更新日志

Version 描述
4.4.0 Added the $tagnames parameter.
2.5.0 Introduced.

相关函数

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


  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' );
    

让小雨知道,这篇文章帮到了你
扫码关注微信公众号zs40086(微搜片)随时随地微信看片,抢先福利电影等你来

热门推荐

如有疑问,请前往问答中心反馈!

反馈