Get the Playlist embedded in Post/Page content

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 )
    );
} );