2

At the moment, the audio shortcode only allows four attributes, src, loop, autoplay and preload. When you upload an audio file however, it comes with pretty useful meta data such as the album art, artist, year and so on which would be great if it could be displayed as well. I've been looking for a way to extend the audio shortcode so that the meta data can be included in the shortcode as well.

So far I have stumbled across shortcode_atts_{$shortcode} which can be used to filter existing shortcodes, but apparently one can only filter existing attributes, not add new ones. I'm not looking to create a new shortcode btw, but add to or extend the existing one, so a user doesn't have to use a new shortcode to get this effect. Is there anyway one can achieve this? I'm grateful for any pointers.

  • 1
    A simple alternate is to use the playlist shortcode instead (ie use the "Create Audio Playlist" option when inserting media). The audio shortcode seems to be designed to be a simple cross-platform dropin via mediaelement for the standard HTML5 audio element with standard attributes. – bonger Oct 3 '14 at 10:47
  • Hmm, even though this isn't what I had in mind, this is certainly a headache-free workaround. Although one would think that making a one track playlist kind of defeats the purpose of a playlist. Thanks for the suggestion, though! I'm still keen on finding out if there are any ideas about the actual audio shortcode. – charlenemasters Oct 3 '14 at 13:34
1

I would use the filter wp_audio_shortcode:

/**
 * Filter the audio shortcode output.
 *
 * @since 3.6.0
 *
 * @param string $html    Audio shortcode HTML output.
 * @param array  $atts    Array of audio shortcode attributes.
 * @param string $audio   Audio file.
 * @param int    $post_id Post ID.
 * @param string $library Media library used for the audio shortcode.
 */
return apply_filters( 'wp_audio_shortcode', $html, $atts, $audio, $post_id, $library );

It seems that $audio is not a string as stated but an object of type WP_Post, so you could use $audio->ID to get the meta values.

function wpse_163324_audio_filter( $html, $atts, $audio, $post_id, $library ) {
    // Use something like
    // $meta_values = get_post_meta( $audio->ID, 'your_audio_metas', true );
    // to get the meta values and add them to the var $html like
    // $html .= '<div>your_content</div>';
    return $html;
}
add_filter( 'wp_audio_shortcode', 'wpse_163324_audio_filter', 10, 5 );
  • Thanks for your answer! I have managed to use get_post_meta() to get meta values such as artist name, album and so on with the help of get_post_meta( $audio->ID, '_wp_attachment_metadata', true ); Could you elaborate a little bit further on how I I can get the function you posted to filter the shortcode? What do you mean with "add them to the vat $html"? How do I do this? Sorry if my questions sound silly, but I'm still learning... – charlenemasters Oct 16 '14 at 9:04
  • No problem. $html contains already the complete HTML created by the shortcode but you can add your own HTML to this variable like so $html .= '<div>your_content</div>';. – realloc Oct 16 '14 at 10:42
  • Thanks again. So, I've run into one more issue. I can't actually use $audio->ID to get the ID since it seems like $audio is not actually an object of WP_Post. I tried the whole thing by hardcoding an attachment ID and it worked, but as soon as I change back to $audio->ID I get the error Trying to get property of non-object Any ideas? – charlenemasters Oct 17 '14 at 15:32
  • Please send me your code via mail. We'll find a solution together. ;) – realloc Oct 18 '14 at 17:20
0

I wanted to follow-up on this. For some reason, the $audio variable was coming up blank. I used the $atts["mp3"] value and searched the DB to find the media ID, which I then used to grab meta data based on the answer provided by charlenemasters

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.