get_media_embedded_in_content() WordPress Function

The get_media_embedded_in_content() function allows you to retrieve media objects embedded in the content of a post. This function returns an array of media objects, which you can then iterate over and display in your theme.

get_media_embedded_in_content( string $content, string[] $types = null ) #

Check the content HTML for a audio, video, object, embed, or iframe tags.


Parameters

$content

(string)(Required)A string of HTML which might contain media elements.

$types

(string[])(Optional)An array of media types: 'audio', 'video', 'object', 'embed', or 'iframe'.

Default value: null


Top ↑

Return

(string[]) Array of found HTML media elements.


Top ↑

Source

File: wp-includes/media.php

function get_media_embedded_in_content( $content, $types = null ) {
	$html = array();

	/**
	 * Filters the embedded media types that are allowed to be returned from the content blob.
	 *
	 * @since 4.2.0
	 *
	 * @param string[] $allowed_media_types An array of allowed media types. Default media types are
	 *                                      'audio', 'video', 'object', 'embed', and 'iframe'.
	 */
	$allowed_media_types = apply_filters( 'media_embedded_in_content_allowed_types', array( 'audio', 'video', 'object', 'embed', 'iframe' ) );

	if ( ! empty( $types ) ) {
		if ( ! is_array( $types ) ) {
			$types = array( $types );
		}

		$allowed_media_types = array_intersect( $allowed_media_types, $types );
	}

	$tags = implode( '|', $allowed_media_types );

	if ( preg_match_all( '#<(?P<tag>' . $tags . ')[^<]*?(?:>[\s\S]*?<\/(?P=tag)>|\s*\/>)#', $content, $matches ) ) {
		foreach ( $matches[0] as $match ) {
			$html[] = $match;
		}
	}

	return $html;
}


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.

Show More
Show More