has_shortcode()

最后更新于:2021-11-27 05:13:46

has_shortcode( string$content, string$tag)

Whether the passed content contains the specified shortcode

参数

$content

(string) (Required) Content to search for shortcodes.

$tag

(string) (Required) Shortcode tag to check.

响应

(bool) Whether the passed content contains the given shortcode.

源文件

文件: gc-includes/shortcodes.php

function has_shortcode( $content, $tag ) {
	if ( false === strpos( $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;
}