has_shortcode()
云策文档标注
概述
has_shortcode() 函数用于检测指定内容中是否包含特定的短代码。它接受内容和短代码标签作为参数,返回布尔值表示是否存在。
关键要点
- 参数:$content(字符串,必需)为要搜索的内容;$tag(字符串,必需)为要检查的短代码标签。
- 返回值:布尔值,表示内容是否包含给定短代码。
- 函数内部逻辑:首先检查内容是否包含 '[' 字符,否则返回 false;然后使用 shortcode_exists() 验证标签是否已注册,再通过 get_shortcode_regex() 进行正则匹配搜索。
- 递归处理:支持嵌套短代码的检测,通过递归调用 has_shortcode() 检查内部内容。
- 性能注意:扫描大量内容时可能消耗较多资源,建议谨慎使用。
- 相关函数:shortcode_exists() 用于检查短代码是否注册;get_shortcode_regex() 用于获取搜索正则表达式。
- 引入版本:WordPress 3.6.0。
代码示例
// 示例1:根据短代码使用情况加载脚本
function wpdocs_shortcode_scripts() {
global $post;
if ( is_a( $post, 'WP_Post' ) && has_shortcode( $post->post_content, 'wpdocs-shortcode') ) {
wp_enqueue_script( 'wpdocs-script');
}
}
add_action( 'wp_enqueue_scripts', 'wpdocs_shortcode_scripts');
// 示例2:简单内容检测
$content = 'This is some text, (perhaps pulled via $post->post_content). It has a shortcode.';
if ( has_shortcode( $content, 'gallery' ) ) {
// 内容包含短代码,执行相应操作
}
// 示例3:根据短代码重定向未登录用户
function wpdocs_wordpress_doc_head() {
global $post;
if ( has_shortcode( $post->post_content, 'only_loggedin_users' ) ) {
if ( ! is_user_logged_in() ) {
$page = get_page_by_title('login');
wp_redirect( get_permalink( $page->ID ) );
exit;
}
}
}
add_action( 'template_redirect', 'wpdocs_wordpress_doc_head', 5 );注意事项
- 避免使用 global $post 和 is_a() 检查,可直接使用 get_the_content() 简化代码。
- has_shortcode() 在扫描大量内容时可能影响性能,需注意优化使用场景。
原文内容
Determines whether the passed content contains the specified shortcode.
Parameters
$contentstringrequired-
Content to search for shortcodes.
$tagstringrequired-
Shortcode tag to check.
Source
function has_shortcode( $content, $tag ) {
if ( ! str_contains( $content, '[' ) ) {
return false;
}
if ( shortcode_exists( $tag ) ) {
preg_match_all( '/' . get_shortcode_regex() . '/', $content, $matches, PREG_SET_ORDER );
if ( empty( $matches ) ) {
return false;
}
foreach ( $matches as $shortcode ) {
if ( $tag === $shortcode[2] ) {
return true;
} elseif ( ! empty( $shortcode[5] ) && has_shortcode( $shortcode[5], $tag ) ) {
return true;
}
}
}
return false;
}
Changelog
| Version | Description |
|---|---|
| 3.6.0 | Introduced. |
Skip to note 4 content
Codex
Enqueue some script when some post uses some shortcode.
Note:
has_shortcode()can use a lot of resources if scanning a lot of content./** * Enqueue the wpdocs-script if the wpdocs-shortcode is being used */ function wpdocs_shortcode_scripts() { global $post; if ( is_a( $post, 'WP_Post' ) && has_shortcode( $post->post_content, 'wpdocs-shortcode') ) { wp_enqueue_script( 'wpdocs-script'); } } add_action( 'wp_enqueue_scripts', 'wpdocs_shortcode_scripts');Skip to note 5 content
Ahir Hemant
Simple Example
Redirect to specific page for not logged in user for specific shortcode
function wpdocs_wordpress_doc_head() { global $post; if ( has_shortcode( $post->post_content, 'only_loggedin_users' ) ) { if ( ! is_user_logged_in() ) { $page = get_page_by_title('login'); wp_redirect( get_permalink( $page->ID ) ); exit; } } } add_action( 'template_redirect', 'wpdocs_wordpress_doc_head', 5 );Skip to note 6 content
Codex
Simple Example
Note:
has_shortcode()can use a lot of resources if scanning a lot of content.$content = 'This is some text, (perhaps pulled via $post->post_content). It has a shortcode.'; if ( has_shortcode( $content, 'gallery' ) ) { // The content has a short code, so this check returned true. }