1

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. – Marcos Di Paolo 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. – Marcos Di Paolo 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
2

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>";
    var_dump($metadata);
    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) {
  [0]=>
  array(16) {
    ["dataformat"]=>
    string(3) "mp3"
    ["channels"]=>
    int(2)
    ["sample_rate"]=>
    int(44100)
    ["bitrate"]=>
    int(128000)
    ["channelmode"]=>
    string(12) "joint stereo"
    ["bitrate_mode"]=>
    string(3) "cbr"
    ["lossless"]=>
    bool(false)
    ["encoder_options"]=>
    string(6) "CBR128"
    ["compression_ratio"]=>
    float(0.090702947845805)
    ["fileformat"]=>
    string(3) "mp3"
    ["filesize"]=>
    int(458918)
    ["mime_type"]=>
    string(10) "audio/mpeg"
    ["length"]=>
    int(29)
    ["length_formatted"]=>
    string(4) "0:29"
    ["artist"]=>
    string(6) "MY_ARTIST"
    ["album"]=>
    string(5) "MY_ALBUM"
  }
}

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

object(WP_Post)#587 (24) {
  ["ID"]=>
  int(198)
  ["post_author"]=>
  string(1) "1"
  ["post_date"]=>
  string(19) "2017-09-26 15:54:30"
  ["post_date_gmt"]=>
  string(19) "2017-09-26 15:54:30"
  ["post_content"]=>
  string(38) ""My test audio"."
  ["post_title"]=>
  string(35) "My test audio"
  ["post_excerpt"]=>
  string(10) "MY_CAPTION"
  ["post_status"]=>
  string(7) "inherit"
  ["comment_status"]=>
  string(4) "open"
  ["ping_status"]=>
  string(6) "closed"
  ["post_password"]=>
  string(0) ""
  ["post_name"]=>
  string(35) "my-test-audio"
  ["to_ping"]=>
  string(0) ""
  ["pinged"]=>
  string(0) ""
  ["post_modified"]=>
  string(19) "2017-09-26 17:36:59"
  ["post_modified_gmt"]=>
  string(19) "2017-09-26 17:36:59"
  ["post_content_filtered"]=>
  string(0) ""
  ["post_parent"]=>
  int(176)
  ["guid"]=>
  string(97) "http://my_testsite.com/wp-content/uploads/sites/4/2017/03/my-test-audio.mp3"
  ["menu_order"]=>
  int(0)
  ["post_type"]=>
  string(10) "attachment"
  ["post_mime_type"]=>
  string(10) "audio/mpeg"
  ["comment_count"]=>
  string(1) "0"
  ["filter"]=>
  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.