functions.php script breaks when variable isn’t hard coded

I have written a jQuery script that takes a JSON encoded list produced with this function is run in my theme’s functions.php and creates a playlist for my jPlayer. However, the script only works when the $file variable is hard coded (for example, OH0400). But I need it to pick up the $file variable based on the page being loaded. But when I switch to this method (using URL), the script says the JSON is null.

I’ve run the script in multiple ways and the output between the hard coded $file and the variable based $file appear to be the same. Why do I get null when I make the switch?

Read More

Here’s the PHP in my theme functions.php.

 function MyjPlayerList(){
    $url = explode( '/', $_SERVER['REQUEST_URI'] );
    $file = strtoupper($url[2]);
    //$file = 'OH0400';

    $filename = '/dir/oralhistory/mp3files/'.$file.'*.mp3';
    $FILES = glob( $filename );
          foreach( $FILES as $key => $mp3 )  {
          $mp3 = str_replace( '/dir/oralhistory/mp3files/', '',$mp3);           
          $FILE_LIST[ $key ][ 'title' ] = $mp3;
          $FILE_LIST[ $key ][ 'mp3' ] = 'http://websiteurl.org/mp3files/'.$mp3;   
    } 

    $myjplayerdata = json_encode( $FILE_LIST ); 
    header ( 'Content-type: application/json' );    
    echo $myjplayerdata;
    exit;
    die();
     };

Here is my javascript:

    ajax_player = function() {
        jQuery('div#player').load('/js/player.html' , function() {
            var cssSelector= {
                jPlayer: "#jquery_jplayer_1",
                cssSelectorAncestor: "#jp_container_1"
            };
        var playlist = [];
        var options = {
            swfPath: "/js/Jplayer.swf",
            supplied: "mp3",
            smoothPlayBar: true,
            keyEnabled: true
        };
        var myPlaylist = new jPlayerPlaylist(cssSelector, playlist, options);
         jQuery.ajax({
                  url: "/wp-admin/admin-ajax.php" ,
                  type: "POST",
                  dataType: "text json",
                  data: { action: "MyjPlayerList"},
                  success:(function(data) {
                jQuery.each(data, function(index, value){
                        myPlaylist.add(value); // add each element in data in myPlaylist
                      console.log(data);        
                })
                  })//function (data) close

                })//ajax close  
        })//jquery.load  
}//ajax_player

Related posts

Leave a Reply

2 comments

  1. thanks to the debugging of Marc,turns out that what I get when i run the script in page & what i get when I call the script w/ javascript are different. it’s trying to glob admin-ajax.php instead of the URL.