get_media_embedded_in_content

函数
get_media_embedded_in_content ( $content, $types = null )
参数
  • (string) $content A string of HTML which might contain media elements.
    Required:
  • (string[]) $types An array of media types: 'audio', 'video', 'object', 'embed', or 'iframe'.
    Required:
    Default: null
返回值
  • (string[]) Array of found HTML media elements.
定义位置
相关方法
get_url_in_contentget_media_itemget_the_contentget_media_itemsget_comment_delimited_block_content
引入
3.6.0
弃用
-

get_media_embedded_in_content函数用于从一个文章或页面的内容中检索嵌入式媒体对象(如图像、视频、音频文件)的阵列: 这个函数可以用来显示包含在一个文章或页面内容中的媒体。

检查HTML内容中的音频、视频、对象、嵌入或iframe标签。

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 . ')[^<]*?(?:>[sS]*?</(?P=tag)>|s*/>)#', $content, $matches ) ) {
		foreach ( $matches[0] as $match ) {
			$html[] = $match;
		}
	}

	return $html;
}

常见问题

FAQs
查看更多 >