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

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()?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

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

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x