4

I'm using the beautiful shortcode "playlist" in my site but I want the random playing of my audio files.

What I use is this:

[playlist tracklist="false" images="false" artists="false" ids="7691,7690,7688,7687,7686,7685,7684,7683,7682,7681,7680,7679,7678,7677,7676,7674,3978,3647,3408,1395,1217,1216,1214,1213,1211,1210,1209,1208,1207,1206,1181,975,899,668,663,642,641"]

How to make random playing, maybe also with dad jQuery?

  • 2
    Note for readers of this question: There's a patch waiting for WP 4.1. Codex has been updated to reflect that. – kaiser Sep 12 '14 at 0:47
4

I removed all that duplicated and unnecessary stuff, take a look at otto's or kaiser's answers for the very simple solution.

5

It looks like there is a both a bug and that the documentation was written incorrectly in the core code. However, there is a workaround that should work for the future and for any fixes that may be made to core.

It's a simple one too:

Use orderby="rand" in the shortcode.

Just that easy.

[playlist orderby="rand" tracklist="false" images="false" artists="false" ids="7691,7690,7688,7687,7686,7685,7684,7683,7682,7681,7680,7679,7678,7677,7676,7674,3978,3647,3408,1395,1217,1216,1214,1213,1211,1210,1209,1208,1207,1206,1181,975,899,668,663,642,641"]

  • 1
    That's what I've written in my answer. ;) Extended Codex with it now. – kaiser Sep 12 '14 at 0:32
4

Here's a shuffle

add_action( 'wp_footer', function () {
?>
<script>
jQuery(function ($) {
    var playlist = $('.wp-playlist'); // Tune this if more than one.
    var tracks = $('.wp-playlist-tracks .wp-playlist-item', playlist);
    var last_shuffle = -1;
    var shuffle = function () {
        var i = Math.floor(Math.random() * tracks.length);
        if (i == last_shuffle) i = (i + 1) % tracks.length;
        last_shuffle = i;
        tracks.get(i).click();
    };
    shuffle();
    $('.mejs-mediaelement audio', playlist).on('ended', function (event) {
        shuffle();
    });
});
</script>
<?php
});
3

Random order

From what I can read in source, there's one argument responsible for this:

'order' => 'RAND',

In the case you use this, the orderby value will be set to none.

Edit

Ok, core code tells that this should work:

if ( 'RAND' == $atts['order'] ) {
    $atts['orderby'] = 'none';
}

But this is not true. In fact, you have to set the orderby value to rand, as the shortcode calls get_posts() and this is what returns posts in random order. In short: What core does with RAND is (afaik) completely unnecessary (and confusing).

Default and always set arguments

If you want to filter the shortcode attributes for all playlist shortcode calls, then go with the following:

add_action( 'shortcode_atts_playlist', function( $output, $defaults, $custom )
{
    // do some array_merge() or wp_parse_args() here
    return $output;
}, 10, 3 );

Custom override

If you want to override the playlist shortcode with a completely custom shortcode, go with shortcircuiting it - return the first argument of the following filter as anything, but not an empty string:

add_filter( 'post_playlist', function( $playlist = '', $attr )
{
    return $playlist;
}, 10, 2 );

Debugging shortcodes

If you hit a wall and some argument isn't working, then it's probably a plugins or themes fault. Take a look at shortcode_atts(). Then dump the contents of the filter inside the callback I've shown above.

  • Too complicated. I just want to play random in my shortcode PLAYLIST. – ginolin Sep 11 '14 at 18:27
  • 3
    Too complicated ? Welcome to WordPress Development. – kaiser Sep 11 '14 at 18:28

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