WordPress at a glance

has_shortcode() WP 1.0

Whether the passed content contains the specified shortcode

  • Global. Array. $shortcode_tags
Is the basis for: get_post_galleries()
✈ 1 time = 0.000014s = very fast | 50000 times = 0.03s = speed of light PHP 7.0.8, WP 4.6.1

No Hooks.

Return

true/false. Whether the passed content contains the given shortcode.

Usage

has_shortcode( $content, $tag );
$content(string) (required)
Content to search for shortcodes.
$tag(string) (required)
Shortcode tag to check.

Code of has_shortcode: wp-includes/shortcodes.php VER 4.9.8

<?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;
}

Related Functions

From tag: Shortcode

More from tag: Conditional tags (all)

More from category: Shortcodes

No comments
    Hello, !     Log In . Register