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


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!

  • Thanks for both of these approaches. For the first approach, if I do <# console.log(data) #> inside prefix_wp_underscore_playlist_templates() to examine the data object, I don't see attachment_id? – henrywright Jun 21 '17 at 15:47
  • This is untested but this should be in the meta data part in #1. This is constructed by peeking at the core functions. I'm not able to test it yet, but will try it later today ;-) – birgire Jun 21 '17 at 15:56
  • I had a super dumb moment. I was still using the wp_playlist_shortcode() function to output my playlist. I forgot to use the actual [playlist] shortcode. Approach works great, thank you. – henrywright Jun 21 '17 at 16:31
  • great, glad to hear it worked. – birgire Jun 21 '17 at 16:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.