Advertisement
brasofilo

Random player with automatic insertion

Oct 6th, 2011
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.51 KB | None | 0 0
  1. <?php
  2. /*
  3.  * Random player with automatic insertion
  4.  * Install : append the code after the_content()
  5.  * Description : Grabs all audio files attached to the post and prints the Audio Player
  6.  * Dependencies : Plugin Audio Player http://wordpress.org/extend/plugins/audio-player/
  7.  * Observations : this code will be better if converted to a function
  8.  */
  9. the_content();
  10.  
  11.  
  12. // QUERY
  13. $audios =& get_children( 'post_parent='.$post->ID.'&post_type=attachment&post_mime_type=audio' );
  14.  
  15. // IF HAVE ATTACHED AUDIOS AND AVAILABLE FUNCTION
  16. if ( !empty($audios) && function_exists("insert_audio_player")) {
  17.  
  18.     // BASIC VARS
  19.     $echou_ini = "[audio:";
  20.     $echou_aud = "";
  21.     $echou_tit = "|titles=";
  22.     $echou_end = "|autostart=yes]";
  23.     $counter = 0;
  24.     $a = array();
  25.  
  26.     // GRAB VALUES AND MAKE TEMP ARRAY
  27.     foreach ( $audios as $attachment_id => $attachment ) {
  28.         $a[$counter]['url'] = wp_get_attachment_url( $attachment_id );
  29.         $post_id_7 = get_post($attachment_id);
  30.         $a[$counter]['tit'] = $post_id_7->post_title;
  31.         $counter++;
  32.     }
  33.  
  34.     // SHUFFLE TEMP ARRAY AND PREPARE AUX STRINGS
  35.     shuffle($a);
  36.     foreach ( $a as $audio ) {
  37.         $echou_aud .= $audio['url']. ",";
  38.         $echou_tit .= $audio['tit'].",";
  39.     }
  40.  
  41.     // REMOVE EXTRA COMMA
  42.     $echou_aud_new = substr($echou_aud, 0, -1);
  43.     $echou_tit_new = substr($echou_tit, 0, -1);
  44.  
  45.     // MAIN STRING
  46.     $echou_final = $echou_ini . $echou_aud_new . $echou_tit_new . $echou_end;
  47.  
  48.     // PRINT TEST STRING (REMOVE AFTER TEST)
  49.     echo $echou_final;
  50.  
  51.     // INSERT PLAYER
  52.     insert_audio_player( $echou_final );
  53. }
  54. ?>
  55.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement