Skip to content

Instantly share code, notes, and snippets.

@n7studios
Created November 13, 2014 15:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save n7studios/d6f4cee573927202e08b to your computer and use it in GitHub Desktop.
Save n7studios/d6f4cee573927202e08b to your computer and use it in GitHub Desktop.
Envira - Random Image Order
<?php
/**
* Plugin Name: Envira Gallery - Random Image Order
* Plugin URI: http://enviragallery.com
* Version: 1.0
* Author: Tim Carr
* Author URI: http://www.n7studios.co.uk
* Description: Display image thumbnails in a random order
*/
/**
* Shuffle gallery images in the data so they are randomly ordered
*
* @param array $data Gallery data
* @param int $galleryID Gallery ID
* @return array Gallery data
*/
function envira_gallery_random_image_order( $data, $galleryID ) {
// IDs of galleries to randomize
$galleriesToRandomize = array(
391,
392
);
// Check if we need to randomize this gallery
if ( !in_array( $galleryID, $galleriesToRandomize) ) {
return $data;
}
// Shuffle keys
$keys = array_keys($data['gallery']);
shuffle($keys);
// Rebuild array in new order
$new = array();
foreach($keys as $key) {
$new[$key] = $data['gallery'][$key];
}
// Assign back to gallery
$data['gallery'] = $new;
// Return data
return $data;
}
add_action( 'envira_gallery_pre_data', 'envira_gallery_random_image_order', 10, 2 );
@karks88
Copy link

karks88 commented Dec 12, 2014

Tim, I just found this and it is a HUGE help to a project I'm doing. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment