Using WordPress ‘get_media_embedded_in_content()’ PHP function

The get_media_embedded_in_content() WordPress PHP function checks the HTML content for audio, video, object, embed, or iframe tags.

Usage

get_media_embedded_in_content($content, $types = null);

Custom example:

Input:

$content = '<p>Sample content with <iframe src="https://www.example.com"></iframe></p>';
$types = array('iframe');
get_media_embedded_in_content($content, $types);

Output:

<iframe src="https://www.example.com"></iframe>

Parameters

  • $content (string) – A string of HTML which might contain media elements.
  • $types (array) – An array of media types: ‘audio’, ‘video’, ‘object’, ’embed’, or ‘iframe’. Default: null.

More information

See WordPress Developer Resources: get_media_embedded_in_content

Examples

Extract video embeds from content

Extract all video embeds from the post content and display them.

$content = '<p>Sample content with <video src="movie.mp4"></video></p>';
$types = array('video');
$videos = get_media_embedded_in_content($content, $types);
foreach ($videos as $video) {
    echo $video;
}

Extract audio embeds from content

Extract all audio embeds from the post content and display them.

$content = '<p>Sample content with <audio src="song.mp3"></audio></p>';
$types = array('audio');
$audios = get_media_embedded_in_content($content, $types);
foreach ($audios as $audio) {
    echo $audio;
}

Extract object embeds from content

Extract all object embeds from the post content and display them.

$content = '<p>Sample content with <object data="flash.swf"></object></p>';
$types = array('object');
$objects = get_media_embedded_in_content($content, $types);
foreach ($objects as $object) {
    echo $object;
}

Extract embed tags from content

Extract all embed tags from the post content and display them.

$content = '<p>Sample content with <embed src="flash.swf"></embed></p>';
$types = array('embed');
$embeds = get_media_embedded_in_content($content, $types);
foreach ($embeds as $embed) {
    echo $embed;
}

Extract multiple media types from content

Extract all audio, video, and iframe tags from the post content and display them.

$content = '<p>Sample content with <audio src="song.mp3"></audio>, <video src="movie.mp4"></video>, and <iframe src="https://www.example.com"></iframe></p>';
$types = array('audio', 'video', 'iframe');
$media_elements = get_media_embedded_in_content($content, $types);
foreach ($media_elements as $media) {
    echo $media;
}