get post playlist like get_post_gallery()
Is there a function similar to get_post_gallery()
that would retrieve a posts playlist(s)? If not can anyone suggest the best way to get the playlist (audio or video) from a post?
Is there a function similar to get_post_gallery()
that would retrieve a posts playlist(s)? If not can anyone suggest the best way to get the playlist (audio or video) from a post?
We can look at get_post_galleries()
for ideas how to handle playlists instead of galleries.
Here's an untested modification based on the core function:
function wpse_get_post_playlists( $post, $html = true ) {
if ( ! $post = get_post( $post ) )
return array();
if ( ! has_shortcode( $post->post_content, 'playlist' ) )
return array();
$playlists = array();
if ( preg_match_all( '/' . get_shortcode_regex() . '/s', $post->post_content, $matches, PREG_SET_ORDER ) ) {
foreach ( $matches as $shortcode ) {
if ( 'playlist' === $shortcode[2] ) {
$ids = array();
$shortcode_attrs = shortcode_parse_atts( $shortcode[3] );
if ( ! is_array( $shortcode_attrs ) ) {
$shortcode_attrs = array();
}
// Specify the post id of the playlist we're viewing if the shortcode doesn't reference another post already.
if ( ! isset( $shortcode_attrs['id'] ) ) {
$shortcode[3] .= ' id="' . intval( $post->ID ) . '"';
}
$playlist = do_shortcode_tag( $shortcode );
if ( $html ) {
$playlists[] = $playlist;
} else {
preg_match_all( '#ids=([\'"])(.+?)\1#is', $playlist, $ids, PREG_SET_ORDER );
if ( ! empty( $ids ) ) {
foreach ( $ids as $id ) {
$ids[] = $id[2];
}
}
$playlists[] = array_merge(
$shortcode_attrs,
array(
'ids' => array_values( array_unique( $ids ) )
)
);
}
}
}
}
return $playlists;
}
Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.