get_shortcode_tags_in_content( string $content ): string[]

Returns a list of registered shortcode names found in the given content.

Description

Example usage:

get_shortcode_tags_in_content( '
 [foo] ' );
// array( 'audio', 'gallery' )

Parameters

$contentstringrequired
The content to check.

Return

string[] An array of registered shortcode names found in the content.

Source

function get_shortcode_tags_in_content( $content ) {
	if ( false === strpos( $content, '[' ) ) {
		return array();
	}

	preg_match_all( '/' . get_shortcode_regex() . '/', $content, $matches, PREG_SET_ORDER );
	if ( empty( $matches ) ) {
		return array();
	}

	$tags = array();
	foreach ( $matches as $shortcode ) {
		$tags[] = $shortcode[2];

		if ( ! empty( $shortcode[5] ) ) {
			$deep_tags = get_shortcode_tags_in_content( $shortcode[5] );
			if ( ! empty( $deep_tags ) ) {
				$tags = array_merge( $tags, $deep_tags );
			}
		}
	}

	return $tags;
}

Changelog

VersionDescription
6.3.2Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.