Title: [Plugin: Audio Player] Random player with automatic insertion
Last modified: August 19, 2016

---

# [Plugin: Audio Player] Random player with automatic insertion

 *  [brasofilo](https://wordpress.org/support/users/brasofilo/)
 * (@brasofilo)
 * [16 years, 2 months ago](https://wordpress.org/support/topic/plugin-audio-player-random-player-with-automatic-insertion/)
 * this code will insert a random playlist with all the audio files attached to 
   a page
 * i’m using it in a template page inserting this code after the_content
 *     ```
       <?php the_content(); ?>
   
       <?php
       // QUERY
       $audios =& get_children( 'post_parent='.$post->ID.'&post_type=attachment&post_mime_type=audio' );
   
       // IF HAVE ATTACHED AUDIOS AND AVAILABLE FUNCTION
       if ( !empty($audios) && function_exists("insert_audio_player")) {
   
       	// BASIC VARS
       	$echou_ini = "[audio:";
       	$echou_aud = "";
       	$echou_tit = "|titles=";
       	$echou_end = "|autostart=yes]";
       	$counter = 0;
       	$a = array();
   
       	// GRAB VALUES AND MAKE TEMP ARRAY
       	foreach ( $audios as $attachment_id => $attachment ) {
       		$a[$counter]['url'] = wp_get_attachment_url( $attachment_id );
       		$post_id_7 = get_post($attachment_id);
       		$a[$counter]['tit'] = $post_id_7->post_title;
       		$counter++;
       	}
   
       	// SHUFFLE TEMP ARRAY AND PREPARE AUX STRINGS
       	shuffle($a);
       	foreach ( $a as $audio ) {
       		$echou_aud .= $audio['url']. ",";
       		$echou_tit .= $audio['tit'].",";
       	}
   
       	// REMOVE EXTRA COMMA
       	$echou_aud_new = substr($echou_aud, 0, -1);
       	$echou_tit_new = substr($echou_tit, 0, -1);
   
       	// MAIN STRING
       	$echou_final = $echou_ini . $echou_aud_new . $echou_tit_new . $echou_end;
   
       	// PRINT TEST STRING (REMOVE AFTER TEST)
       	echo $echou_final;
   
       	// INSERT PLAYER
       	insert_audio_player( $echou_final );
       }
       ?>
       ```
   

Viewing 8 replies - 1 through 8 (of 8 total)

 *  [pookie13z](https://wordpress.org/support/users/pookie13z/)
 * (@pookie13z)
 * [16 years, 1 month ago](https://wordpress.org/support/topic/plugin-audio-player-random-player-with-automatic-insertion/#post-1453582)
 * Can you explain where I’m supposed to insert my info? I’m a newbie so insight
   into where my URLs and titles go would be great. Are they separated by commas?
   Thanks!
 *  [Mike Smith](https://wordpress.org/support/users/daledubilowski/)
 * (@daledubilowski)
 * [15 years, 11 months ago](https://wordpress.org/support/topic/plugin-audio-player-random-player-with-automatic-insertion/#post-1453612)
 * Very interesting… Brasofilo, do you think this is somehow possible:
 * User presses play on the player – the player grabs a random mp3 hosted in an 
   ADS folder on the site (5 second audio ads) – plays the ad and then plays the
   intended .mp3 audio file.
 * Basically looking to add a 5 second audio ad (chosen at random from a separate
   ads folder) before playing the intended mp3 file.
 * Thoughts? Is there another audio player / ad server out there that might be able
   to accomplish this?
 *  [tsenglabs](https://wordpress.org/support/users/tsenglabs/)
 * (@tsenglabs)
 * [15 years, 7 months ago](https://wordpress.org/support/topic/plugin-audio-player-random-player-with-automatic-insertion/#post-1453635)
 * Great!!
    I unfortunately do not know why, it does not work ….. I have changed….
   here is an alternate version, modified by me
 *  <?php
    // file name 1.mp3 2.mp3 3.mp3 4.mp3 5.mp3 etc… // modify range (1,5)
   ES. (1,10) if you have also 6.mp3 7.mp3 8.mp3 9.mp3 10.mp3
 * srand((double)microtime()*1000000);
    $fabio =rand(1,5);
 * // QUERY
    $audios =& get_children( ‘post_parent=’.$post->ID.’&post_type=attachment&
   post_mime_type=audio’ );
 * // IF HAVE ATTACHED AUDIOS AND AVAILABLE FUNCTION
    if (function_exists(“insert_audio_player”)){
 *  // BASIC VARS
    $echou_ini = “[audio:”; $echou_aud = “”; $echou_tit = “.mp3”;
   $echou_end = “|autostart=yes]”; $counter = 0; $a = array();
 *  // GRAB VALUES AND MAKE TEMP ARRAY
    foreach ( $audios as $attachment_id => $
   attachment ) { $a[$counter][‘url’] = wp_get_attachment_url( $attachment_id );
   $post_id_7 = get_post($attachment_id); $a[$counter][‘tit’] = $post_id_7->post_title;
   $counter++; }
 *  // SHUFFLE TEMP ARRAY AND PREPARE AUX STRINGS
    shuffle($a); foreach ( $a as 
   $audio ) { $echou_aud .= $audio[‘url’]. “,”; $echou_tit .= $audio[‘tit’].”,”;}
 *  // REMOVE EXTRA COMMA
    $echou_aud_new = substr($echou_aud, 0, -1); $echou_tit_new
   = substr($echou_tit, 0, -1);
 *  // MAIN STRING
    $echou_final = $echou_ini . $fabio . $echou_tit . $echou_end;
 *  // PRINT TEST STRING (REMOVE AFTER TEST)
    //echo $echou_final;
 *  // INSERT PLAYER
    insert_audio_player( $echou_final ); } ?>
 *  [SB](https://wordpress.org/support/users/sambritt/)
 * (@sambritt)
 * [15 years, 3 months ago](https://wordpress.org/support/topic/plugin-audio-player-random-player-with-automatic-insertion/#post-1453659)
 * Can this code be used with the widget plugin that goes with this audio player?
   🙂
 * [http://wordpress.org/extend/plugins/audio-player-widget/](http://wordpress.org/extend/plugins/audio-player-widget/)
 * Can it also play from a directory on the server? Why does this player verify 
   a music directory if it doesn’t do anything with it?
 * Would be cool to see the widget plugin just do the random and play it from a 
   directory or an xml file.
 * Thank you kindly, 🙂
 *  [tsenglabs](https://wordpress.org/support/users/tsenglabs/)
 * (@tsenglabs)
 * [15 years, 3 months ago](https://wordpress.org/support/topic/plugin-audio-player-random-player-with-automatic-insertion/#post-1453660)
 * Yes this code should be used with this widget plugin! 🙂
    Yes, can it also play
   from a directory on the server, also random: rand(1,5)->play the file 1.mp3 2.
   mp3 3.mp3 4.mp3 5.mp3 …you have to rename the file so! you can change the number
   up to what you want. es: rand(1,100) for 100 song: 1.mp3 2.mp3 ………………. 99.mp3
   100.mp3
 *  [kidkilowatt](https://wordpress.org/support/users/kidkilowatt/)
 * (@kidkilowatt)
 * [15 years, 3 months ago](https://wordpress.org/support/topic/plugin-audio-player-random-player-with-automatic-insertion/#post-1453661)
 * I have my track names/numbers in a post…
    `[audio:track1.mp3,track2.mp3,track3.
   mp3|titles=Song 1,Song 2,Song 3|artists=Artist 1, Artist 2, Artist 3]`
 * and I display the post within my header file.
    `<?php $postslist = get_posts('
   include=68'); ?>`
 * How can I adjust the random code to work with that specific post?
    Thanks!!
 *  [ayllonda](https://wordpress.org/support/users/ayllonda/)
 * (@ayllonda)
 * [15 years, 2 months ago](https://wordpress.org/support/topic/plugin-audio-player-random-player-with-automatic-insertion/#post-1453663)
 * Is there any way this can be used with the standalone player? Or another version
   of this, perhaps? I would love to randomize the songs I have I have on my site
   using this player.
 *  Thread Starter [brasofilo](https://wordpress.org/support/users/brasofilo/)
 * (@brasofilo)
 * [15 years, 2 months ago](https://wordpress.org/support/topic/plugin-audio-player-random-player-with-automatic-insertion/#post-1453665)
 * somehow, i was not subscribed to this post, ups…
 * ayllonda, how are you using it as a standalone player?
 * but, probably you’d have to insert this on your php file:
    `require ('PATH/TO/
   WORDPRESS/wp-load.php');`
 * and instead of using $post->ID, provide the ID of the page/post with the attached
   audios
 * ____________
    nowadays, i would wrap all this code inside a function and place
   it in the theme functions.php so, the code page would look like:
 *     ```
       the_content();
       printPlayer();
       ```
   

Viewing 8 replies - 1 through 8 (of 8 total)

The topic ‘[Plugin: Audio Player] Random player with automatic insertion’ is closed
to new replies.

 * 8 replies
 * 7 participants
 * Last reply from: [brasofilo](https://wordpress.org/support/users/brasofilo/)
 * Last activity: [15 years, 2 months ago](https://wordpress.org/support/topic/plugin-audio-player-random-player-with-automatic-insertion/#post-1453665)
 * Status: not a support question

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
