Displaying Random NextGEN Gallery Images

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 does not seem to be officially supported anymore, so it’s only the question of time how long the rest of the “legacy” will last.

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 now finally doing what it was supposed to be doing! (For the time being, anyway…) Here it is again:

[[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();
	}
}
Share

Posted

in

by

Comments

5 responses to “Displaying Random NextGEN Gallery Images”

  1. Hi:
    Thank you so much for this code! You have refreshed our website and resolved our non-random random display issue! I am grateful 😉

  2. I had this working a few months ago, and now I noticed it isn’t working anymore. I had updated to 2.0.79 not too long ago, so I suspect that may be the issue. All the right code is being called, but still getting cached images.

    I’ll dive into the code and see what I can come up with, but if you have any further tips, feel free to share. 😀

    Thanks!

  3. Cynthia, thanks.

    Javier, see the update I just added to the article.

    I wonder why Photocrati doesn’t just add this functionality already… In support forums, this question pops up on regular basis, but I still haven’t seen any solution…

  4. For the last version it’s working like this:

    wp-config.php
    define(‘PHOTOCRATI_CACHE’, false);

    functions.php

    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();
    }
    }

    Code in the page:
    [random id=1 max=8 order_by=RAND()]

  5. Adrian,

    Indeed it works, even without define(‘PHOTOCRATI_CACHE’, false) or, for that matter, the z_disable_ngg_cache() function at all.

    But like I said at the beginning of this article, the tag random does not seem to be officially supported anymore, so it is unsafe to use it.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.