Advertisement
5ally

wp-stack-exchange-300862

Apr 17th, 2018
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.00 KB | None | 0 0
  1. /**
  2.  * Info about this Paste: https://wordpress.stackexchange.com/a/300886/137402
  3.  * Start copying below this comment, whichever applies.
  4.  */
  5.  
  6. add_action( 'wp_playlist_scripts', function(){
  7.     ?>
  8. <script>
  9. jQuery( function( $ ){
  10.     // Add the search box.
  11.     $( '.wp-playlist' ).prepend(
  12.         '<p class="wp-playlist-custom-search alignright">' +
  13.             '<input class="search-playlist" placeholder="Search" />' +
  14.         '</p>' +
  15.         '<div style="clear: both;"></div>'
  16.     );
  17.  
  18.     // Performs the search.
  19.     $( '.search-playlist', '.wp-playlist' ).on( 'keyup', function( e ){
  20.         var $playlist = $( this ).closest( '.wp-playlist' ),
  21.             query = this.value;
  22.  
  23.         if ( ! query ) {
  24.             $( '.wp-playlist-item', $playlist ).show();
  25.             return;
  26.         } else if ( e.key.length > 1 ) {
  27.             return;
  28.         }
  29.  
  30.         $( '.wp-playlist-item-title', $playlist ).each( function(){
  31.             var re = new RegExp( '(' + query + ')', 'ig' ),
  32.                 title = $( this ).text();
  33.  
  34.             if ( re.test( title ) ) {
  35.                 $( this ).closest( '.wp-playlist-item' ).show();
  36.             } else {
  37.                 $( this ).closest( '.wp-playlist-item' ).hide();
  38.             }
  39.         } );
  40.     } );
  41.  
  42.     // This is an alternative to editing wp-includes/js/mediaelement/wp-playlist.js
  43.     if ( 'function' === typeof window.WPPlaylistView ) {
  44.         // This is a custom function. If the item is visible, plays the track.
  45.         // Otherwise, we go to the next or previous track.
  46.         window.WPPlaylistView.prototype.goTo = function(){
  47.             if ( this.$( '.wp-playlist-item' ).eq( this.index ).is( ':visible' ) ) {
  48.                 this.setCurrent();
  49.                 return true;
  50.             }
  51.         };
  52.  
  53.         window.WPPlaylistView.prototype.ended = function(){
  54.             this.next();
  55.         };
  56.  
  57.         window.WPPlaylistView.prototype.prev = function(){
  58.             this.index = this.index - 1 < 0 ? this.tracks.length - 1 : this.index - 1;
  59.  
  60.             if ( ! this.goTo() ) {
  61.                 this.prev();
  62.             }
  63.         };
  64.  
  65.         window.WPPlaylistView.prototype.next = function(){
  66.             this.index = this.index + 1 >= this.tracks.length ? 0 : this.index + 1;
  67.  
  68.             if ( ! this.goTo() ) {
  69.                 this.next();
  70.             }
  71.         };
  72.     }
  73. } );
  74. </script>
  75.     <?php
  76. } );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement