Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a music player in my theme that generates its playlist from a javascript file that looks like this

var myPlaylist = [
     {
        mp3:'track url goes here',
        title:'title here',
        artist:'artist',
    }
    {
        mp3:'track url goes here',
        title:'title here',
        artist:'artist',
    } 
etc...

];

How can I query the media library so it will echo the track url, title text and maybe an existing audio parameter like "description" for the artist value?

I know enough about php to put them in the right places once they're queried, I just needto know how to pull them from the wp database!

share|improve this question
    
Welcome to WPSE! +1 for a great, well-written question! –  Chip Bennett Feb 26 '12 at 17:37
add comment

2 Answers

up vote 3 down vote accepted

Welcome to WPSE marctain!

Edit There are some critiques on using the guid but no one of the commentators managed to edit this answer to a better one, so I'll do it.

Yes, using guid is a bad idea in the long run, I knew that and I should have pointed that out, I didn't, it was a mistake. I'm sorry, it was a quick and dirty answer.

In my original answer, I would have made usage of wp_get_attachment_url to get the correct url in any case. This adds an extra query, but it is safe to use.

$args = array
    (
        'post_type' => 'attachment',
        'post_mime_type' => 'audio',
        'numberposts' => -1
    );
 $audiofiles = get_posts($args);

 foreach ($audiofiles as $file)
 {
      $url = wp_get_attachment_url($file->ID);
      echo $url; // url
      echo file->post_title; //title
      echo file->post_content; // description
 }
share|improve this answer
    
The above code causes the page to display blank white.. any ideas? –  marctain Feb 26 '12 at 22:07
    
OK, So I'm more of a newb to php than I thouht, but I got the code to work, but post_name generates the URL slug, not the full file path. –  marctain Feb 26 '12 at 23:46
    
Yes, Kaiser edited it to post_name, but you have to use ->guid for the full url to the file. –  ungestaltbar Feb 27 '12 at 0:12
    
ah YES! it works perfectly! –  marctain Feb 27 '12 at 0:18
    
Careful: The GUID doesn’t change if you move WordPress to another domain. It is not a URI, it just happens to look like one. –  toscho Feb 27 '12 at 9:58
show 3 more comments

One step further: Use localize to process from php and access in js

Here's an example that allows you to access & modify your tracklist straight from inside your php files. The additional benefit from this solution is that you can now modify stuff using jQuery.ajax functions 1).

 function load_init_tracks()
 {
      $args = array
      (
        'post_type' => 'attachment',
        'post_mime_type' => 'audio',
        'numberposts' => -1
      );
      $audiofiles = get_posts($args);

      $result = array();
       foreach ( $audiofiles as $file )
      {
           $result['mp3'] = wp_get_attachment_url( $file->ID ); // url
           $result['title'] = $file->post_title //title
           $result['artist'] = $file->post_content // description
      }
      // $result now contains all tracks as multi dimensional array
      return $result;
 }

 function load_track_scripts()
 {
     // register your script, enqueue it and then add localize the result to access it inside your js file:
     wp_register_script( 'mp3tracks', get_stylesheet_directory().'js/your_js_filename.js', array( 'jquery' ), 0, true );
     wp_enqueue_script( 'mp3tracks' );
     wp_localize_script( 
         'mp3tracks',
     'track_list_object',
     array( 
         'ajaxurl'  => admin_url( 'admin-ajax.php' )
        ,'nonce'    => wp_create_nonce( 'mp3_nonce_value' ) 
        ,'action'   => "mp3tracks"
             ,'tracks'  => load_init_tracks()
  );
     // Hook it to some ajax callback (for public and logged in users):
     add_action( 'wp_ajax_nopriv_mp3tracks', 'mp3tracks_cb' );
    add_action( 'wp_ajax_nopriv_mp3tracks', 'mp3tracks_cb' );
 }
 add_action( 'init', 'load_track_scripts' );

 // Now call the function that processes the 
 function mp3tracks_cb()
 {
      check_ajax_referer( 'mp3_nonce_value', 'nonce' );

      // This is what the data you can process with the ajax response
      $data = $_POST;
      # @todo validate your input: esc_attr(), strip_tags(), etc.

    $response = json_encode( array(
         'tracks' => $data
    ) );

    header( "Content-Type: application/json" );
    echo $response;
    exit;
 }

You then can access your result inside your javascript file from track_list_object.data. This ↑ also allows you to process stuff via ajax inside the function.

Just process your stuff during something like:

// Inside theme_root/js/your_js_filename.js
jQuery( document ).on(
    "pageinit"
    ,function( e, data )
    {
        // do stuff... take a look at your "console"-tab in the chrome/IE dev bar or with FF Firebug
        console.log( track_list_object );
    }
);

Notes

1) You don't have to do this, but it's easy and allows you to use ajax in case you want to modify your playlists on the fly.

share|improve this answer
    
would that code go into functions.php? –  marctain Feb 26 '12 at 22:08
    
it also causes a blank page when put into functions.php –  marctain Feb 26 '12 at 22:38
    
Ah, ok. I thought you know what you're doing inside themes/plugins and how to handle php... :/ As you don't know how to handle Ajax and such, I'd say, just read into the questions filed under ajax first. –  kaiser Feb 26 '12 at 22:43
    
my bad.. I guess I know less about php than I thought :)! Thanks for the help, though. –  marctain Feb 26 '12 at 23:01
    
First: Add all the WP_DEBUG stuff (read in codex and here on WPSE about it) to your wp-config.php. A blank page means that your debugging isn't turned on an you're not getting any error output. Second: Check - with your IDE - if there's some typo in the code above ↑. When you fixed that, check your console with the js-log. –  kaiser Feb 26 '12 at 23:21
show 2 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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