Get the Playlist embedded in Post/Page content

I often use get_media_embedded_in_content to get the audio or video embedded inside post or page content. But it doesn't seem to work with playlist. I have tried this code

$main_content = apply_filters( 'the_content', get_the_content() );
$media = get_media_embedded_in_content( $main_content, array(
    'audio',
    'video',
    'playlist',
) );

And nothing inside the audio tag:

 string 'audio controls="controls" preload="none" width="618"/audio'

Is it possible to do that? Thanks for all your helps!

Topic playlist media Wordpress

Category Web


The get_media_embedded_in_content() uses a regex to match the input tags, but the HTML for playlists is not made from a single tag, instead it's constructed with tags like <div>, <script>, <noscript>, <ol>, <li>.

Recently I posted an answer to get post playlist like get_post_gallery() that you can check out, to see if it helps. It introduces the custom wpse_get_post_playlists() function.

We could of course target these parts:

<div class="wp-playlist...">...</div>

with regular expression, but then we have to note that the first instance can contain a script part above it, like:

<!--[if lt IE 9]><script>document.createElement('audio');</script><![endif]-->

Another approach is to wrap the output from playlist shortcodes, with e.g.

<!--wpse-playlist--> ... <!--/wpse-playlist-->

and then use regular expression to extract only these parts within.

Here's an example how to wrap the output of playlist shortcodes:

add_shortcode( 'playlist', function( $attr = [], $content = null )
{
    return sprintf( 
        '<!--wpse-playlist-->%s<!--/wpse-playlist-->',
        wp_playlist_shortcode( $attr )
    );
} );

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.