How to get the ID of an item in an audio playlist?

The question:

I’m using wp_playlist_shortcode() to output an audio playlist. For example:

echo wp_playlist_shortcode( array(
    'ids' => $track_ids
) );

Thanks to the idea in answer 142974, I’m unhooking the wp_underscore_playlist_templates() function and using my own version prefix_wp_underscore_playlist_templates() so I can customise the output of playlist items. For example:

add_action( 'wp_playlist_scripts', function() {
    remove_action( 'wp_footer', 'wp_underscore_playlist_templates', 0 );
    add_action( 'wp_footer', 'prefix_wp_underscore_playlist_templates', 0 );
} );

For now I’m just copying the contents of wp_underscore_playlist_templates() to prefix_wp_underscore_playlist_templates().

Inside my custom prefix_wp_underscore_playlist_templates() function, a data object is made available but I believe this just contains “data” related to the track such as data.meta.artist and data.content.

My question

How can I get the post ID of the track when inside prefix_wp_underscore_playlist_templates()?

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

We don’t have an explicit filter for the data array so here are two workarounds that you might give a try:

Approach #1 – As meta data

We can add attachment_id to the meta data for each playlist item, via the wp_get_attachment_metadata filter, but first we have to register it via the
wp_get_attachment_id3_keys filter.

Here’s an example:

// Add 'attachment_id' as an id3 meta key
add_filter( 'wp_get_attachment_id3_keys', $callback_id3 = function( $fields ) 
{
    $fields['attachment_id'] = 'attachment_id';
    return $fields;
} );

// Add ?attachment_id=123 to the url
add_filter( 'wp_get_attachment_metadata', $callback_metadata = function ( $data, $post_id )
{
    $data['attachment_id'] = (int) $post_id;
    return $data;
}, 10, 2 );

// Output from playlist shortcode
$output = wp_playlist_shortcode( $atts, $content );

// Remove our filter callback
remove_filter( 'wp_get_attachment_url', $callback_metadata );
remove_filter( 'wp_get_attachment_id3_keys', $callback_id3 );

Approach #2 – As query string

Alternatively we can add the ?attachment_id=123 query string to the attachments url, via the wp_get_attachment_url filter.

Here’s an example:

// Add our filter callback to add ?attachment_id=123 to the url
add_filter( 'wp_get_attachment_url', $callback = function ( $url, $post_id )
{
    return add_query_arg( 'attachment_id', (int) $post_id, $url );
}, 10, 2 );

// Output from playlist shortcode
$output = wp_playlist_shortcode( $atts, $content );

// Remove our filter callback
remove_filter( 'wp_get_attachment_url', $callback );       

Hope it helps!


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment