In NextGEN Gallery plugin for WordPress prior to version 2.0, it was possible to display a limited number of random images from one specific gallery — like so:
[[random id="25" max="8"]]
This would output eight random thumbnails from the gallery with ID 25, each time different pictures when the page is refreshed. Unfortunately (“thanks” to the company which acquired the plugin from its original author), starting with version 2.0, some legacy behavior support has ceased. For instance, the code above will still output eight images from the desired gallery, but they would not be random any longer. Besides, the tag random
along with its attributes
So I was wondering how to get the desired result. Again. Of course, I adjusted my shortcode in accordance with the new specification and immediately tried the newly introduced random source:
[[ngg_images gallery_ids="25" source="random"]]
To my surprise, it didn’t work: all images from my gallery #25 were displayed in the usual order. After some reading, I found out that the attributes gallery_ids
and source
are mutually exclusive, with gallery_ids
taking precedence. Leaving the latter out and using only the source
has proven to be misleading, since it would select a random set of images from all available galleries.
After some hithering and thithering, the closest result I’d gotten was this (at the time of writing, mind you, most of the attributes were not documented):
[[ngg_images gallery_ids="25" order_by="RAND()" images_per_page="8" disable_pagination="yes"]]
This gave me almost what I wanted, but NextGEN Gallery caching mechanism got in the way. The selected set of images was indeed random, but the randomness was provided only once — when the cache has been rebuilt, i.e. either after a post update, or after a manual cache cleanup (via admin panel), or after its expiration. Any further page hit produced the same — recently random — results.
Okay, now, how to ensure randomness on each page hit? One way is to add this additional parameter (which is neither documented, by the way) to your query string:
http://www.zeyalabs.ch/labs/photography/?ngg_force_update=1
But this is, well, ugly. And you can’t control it everywhere. (I wonder, is there a way of silently adding parameters to WordPress queries? I tried playing with parse_query
action hook, but my additional parameters never made it to the core… Okay, that’s a different topic.)
Another idea was to disable NGG cache by overriding the corresponding plugin constant before its initialization (because, once defined, a PHP constant cannot be re- or undefined):
define('PHOTOCRATI_CACHE', false);
This worked when I put the line above into wp-config.php
, but that file is there to configure the core and not for solving theme- or plugin-related issues. So I tried the elegant way in my theme’s functions:
add_action('muplugins_loaded', 'z_disable_ngg_cache'); function z_disable_ngg_cache() { define('PHOTOCRATI_CACHE', false); }
muplugins_loaded
is said to be the earliest action hook possible, and I hoped it would do the trick. Well, it didn’t, and I still don’t know why. The registered function simply wouldn’t get invoked, although my theme’s functions include a bunch of similar, perfectly working action and filter associations.
Additionally, I realized I didn’t want the NGG cache to be disabled globally on the whole website, but only for the pages where random collections are displayed.
So in the end I came up with this “direct approach” (you might call it a hack, but it’s not, really — it’s based on a code chunk from NGG plugin’s initialization routine):
add_action('the_post', 'z_disable_ngg_cache'); function z_disable_ngg_cache($post) { if ('name-of-my-post' === $post->post_name && class_exists('C_Photocrati_Cache')) { C_Photocrati_Cache::$do_not_lookup = true; C_Photocrati_Cache::$force_update = true; } }
Obviously, this goes into the theme’s functions. Since I needed this behavior to be post-specific, I hooked it to the_post
and made sure to introduce the necessary conditions.
At last! The new shortcode is
[[ngg_images gallery_ids="25" order_by="RAND()" images_per_page="8" disable_pagination="yes"]]
Beware, though! The support team told me that order_by="RAND()"
might not work on some servers. But I hope it will on yours.
Update
Since around NGG 2.0.6-ish (or maybe even earlier), there is no C_Photocrati_Cache
class. In fact, the whole caching engine seems to have been rewritten. I didn’t dig too deep, but the latest trick I had to apply was this:
C_Photocrati_Transient_Manager::flush();
This is how the whole code chunk within your theme custom functions might look like now (as opposed to the example given above):
add_action('the_post', 'z_disable_ngg_cache'); function z_disable_ngg_cache($post) { if ('name-of-my-post' === $post->post_name && class_exists('C_Photocrati_Transient_Manager')) { C_Photocrati_Transient_Manager::flush(); } }
Leave a Reply