Created
November 13, 2014 15:13
-
-
Save n7studios/d6f4cee573927202e08b to your computer and use it in GitHub Desktop.
Envira - Random Image Order
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Tim, I just found this and it is a HUGE help to a project I'm doing. Thank you!