has_shortcode() WordPress Function

has_shortcode() is a function that checks whether a specified shortcode is present in a given string. It returns a boolean value of true or false.

has_shortcode( string $content, string $tag ) #

Whether the passed content contains the specified shortcode


Parameters

$content

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

$tag

(string)(Required)Shortcode tag to check.


Top ↑

Return

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


Top ↑

Source

File: wp-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;
}


Top ↑

Changelog

Changelog
VersionDescription
3.6.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.