How can i add an audio track in the editor and then access its metadata.
When you upload the audio track before inserting it you can fill in Artist info, track name, album and description.
Is there a way you can access that info to, for example, be able to output a metadata caption. Something like.

Audio Player.
Sting - Fields of gold - 1991.

Is it posible with shortcode attributes? like:

[audio scr="http://mywebsite.com/mytrack.mp3" title="The Title" artist="Artist" Album="album"] ??

Any other way to access it?

  • when you say access it, what do you mean? Can you describe the end goal rather than the process?
    – Tom J Nowell
    Sep 26 '17 at 4:27
  • Question updated. Sorry, I wasn't very clear. Sep 26 '17 at 14:36
  • you want to access the attributes of the shortcode?
    – David Lee
    Sep 26 '17 at 15:30
  • No, I want to add a metadata text to the audio player. But I as long as I read it's not possible. The thing is the playlist isn't working for some reason, there you have artist, track, album info. Sep 26 '17 at 15:37
  • to the audio player? playlist? please try to explain by steps of what you are trying to accomplish, saying audio then playlist its confusing, also the metadata you talk about its the one for the audio shortcode, or for the audio file, everything has metadata @_@
    – David Lee
    Sep 26 '17 at 16:01

Yeah, you can access that data using get_post_meta, remember that almost everything in WordPress is a type of a post, an audio its a wp_attachment type, you just need the ID, also you can make a shortcode, this is an example:

function my_audio_func($atts) {
    $audio_id = $atts['id']; //we need an ID as attribute
    //we get the audio and the metadata
    $metadata = get_post_meta($audio_id, "_wp_attachment_metadata");
    echo "<pre>";
    echo "</pre>";

    //we get the post object to get the URL of the AUDIO
    $audio = get_post($audio_id);
    $audio_src = $audio->guid;//for object values is like this is an object
    $attr = array(
        'src' => $audio_src,
        'loop' => '',
        'autoplay' => '',
        'preload' => 'none'
    //display the audio player
    echo wp_audio_shortcode($attr);
    echo "<br/>";
    //display the metadata ARTIST
    echo $metadata[0]["artist"];//for metadata like this is an array

add_shortcode('my_audio', 'my_audio_func');

in your post use it like this:

[my_audio id="198"]

it will show like this:

enter image description here

this is all the metadata keys, the info you can get:

array(1) {
  array(16) {
    string(3) "mp3"
    string(12) "joint stereo"
    string(3) "cbr"
    string(6) "CBR128"
    string(3) "mp3"
    string(10) "audio/mpeg"
    string(4) "0:29"
    string(6) "MY_ARTIST"
    string(5) "MY_ALBUM"

if you want the caption or name, that would be in the post object:

object(WP_Post)#587 (24) {
  string(1) "1"
  string(19) "2017-09-26 15:54:30"
  string(19) "2017-09-26 15:54:30"
  string(38) ""My test audio"."
  string(35) "My test audio"
  string(10) "MY_CAPTION"
  string(7) "inherit"
  string(4) "open"
  string(6) "closed"
  string(0) ""
  string(35) "my-test-audio"
  string(0) ""
  string(0) ""
  string(19) "2017-09-26 17:36:59"
  string(19) "2017-09-26 17:36:59"
  string(0) ""
  string(97) "http://my_testsite.com/wp-content/uploads/sites/4/2017/03/my-test-audio.mp3"
  string(10) "attachment"
  string(10) "audio/mpeg"
  string(1) "0"
  string(3) "raw"

i commented in the shortcode how you can access values in the object and in the metadata array.


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.