How to use get_media_embedded_in_content function

The get_media_embedded_in_content() function is a handy helper function, though it doesn’t seem to be used in the core (ver. 4.2.2).

Regular expression:

To understand the get_media_embedded_in_content() function, we must understand the following regular expression:

#<(?P<tag>video|audio|object|embed|iframe)[^<]*?(?:>[\s\S]*?<\/(?P=tag)>|\s*\/>)#

There are many online regular-expression tools to help us with that.

Like this one:

Regular expression visualization

Debuggex demo

We note that it’s possible to label a capturing group (...) with
(?P<Label>...) and match it with (?P=Label).

Example:

Let’s consider the following string:

$content="
    My page: <iframe src="https://wordpress.stackexchange.com/questions/190530/page.html"></iframe> 
    My favorite song: <audio src="song.mp3"/>
    My old car: <img src="car.jpg"/>
";

How do we extract the HTML code for the embedded media?

We use:

$media = get_media_embedded_in_content( $content );

where the default content media tags are video, audio, object, embed and iframe.

Let’s view the output of:

print_r( $media );

In this case it’s:

Array
(
    [0] => <iframe src="https://wordpress.stackexchange.com/questions/190530/page.html"></iframe>
    [1] => <audio src="song.mp3"/>
)

We note that the img tag isn’t included, as expected.

oEmbeds:

If we need to catch the oembeds in the content, we should use for example:

$media = get_media_embedded_in_content( 
    apply_filters( 'the_content', get_the_content() )
);

instead of:

$media = get_media_embedded_in_content( get_the_content() );

in the loop, because get_the_content() returns the unfiltered content.

Filter:

We can control the allowed content media tags, with the media_embedded_in_content_allowed_types filter.

Example:

/**
 * Remove 'object' from the allowed content media types.
 * 
 * @param  array $types Array of allowed types
 * @return array $types Array of allowed types
 */
add_filter( 'media_embedded_in_content_allowed_types', function( $types )
{
    // Let's remove 'object'
    if( isset( $types['object'] ) )
        unset( $types['object'] );

    return $types;
} );

Leave a Comment