• wpress2010

    (@wpress2010)


    I’ve got six FooGallery albums on a page, each one containg a single Gallery. Each Gallery has its sort set to Random. If I go to each Gallery’s page, and refresh, the order of the thumbnails changes as the Random sort is applied – perfect.

    Each Album contains a single thumbnail, representing the Gallery that is inside the Album. But when I refresh the Album’s page, the Gallery thumbnail does not change – it’s always the same image. Is there a way to enable the Gallery’s random thumbnail image selection in the Album? If not, maybe a good candidate for a future release, or a Pro version?

Viewing 7 replies - 1 through 7 (of 7 total)
  • I have been looking for this too. There appears to be no way to randomly sort thumbnails upon each page load. I have searched Google, looked at filters, checked for extensions, etc. This seems basic so I must have missed it.

    Sorting randomly on the gallery edit page is a 1 time thing and not at all what I want. Randomly ordering on each page load keeps a gallery fresh for visitors.

    Thread Starter wpress2010

    (@wpress2010)

    I agree that having the random sort with each page load keeps the gallery looking “fresh”. I had this as a standard feature on some Zen Gallery sites I wrote some years ago. It was built into the software.

    Doesn’t seem to be any response from FooGallery’s developers, unfortunately…

    The problem appears to be a design flaw. Fully formed gallery HTML is cached in postmeta (“foogallery_cache” meta_keys). There is no filter to intercept it. Full URLs are embedded which is an annoyance for site portability (just click update on the gallery to recreate).

    IMHO only thumbnail data with relative paths should be saved in an easily parsed format with a filter hook.

    For now, I am patching the plugin to add a filter hook in class-foogallery-cache.php changing:

    echo $gallery_cache;
    to:
    echo apply_filters( 'foogallery_meta_cache', $gallery_cache );

    Then using it (e.g. in functions.php):

    add_filter('foogallery_meta_cache', function($cache) {
    	$lines = preg_replace(
    		['@(\r|\n|\\\\r|\\\\n|\\\\t)@', '@(<a|</div)@', '@https?://[^/]*@'],
    		['', "\n$1", ''], $cache);
    	$thumbs = preg_grep('/<a/', preg_split("/\n/", $lines));
    	shuffle($thumbs);
    	return preg_replace('/(<div[^>]*>).*/s', "\n\n$1", $cache) .
    		implode("\n", $thumbs) . "\n</div>\n";
    });
    Plugin Author bradvin

    (@bradvin)

    hey guys,

    Thanks for posting about this issue. The new HTML caching feature will indeed make the random sorting functionality void. I did not think about this scenario so thanks for pointing it out. I can think of 2 ways to solve this:

    1) Disable HTML cache in your FooGallery settings, so it returns to how it used to work before HTML caching was introduced. You will be able to do this now without waiting for an update.
    2) If you choose “random” sorting for a gallery, then I automatically override the HTML cache for that specific gallery and disable it. You will have to wait for an update for this.

    @rebelga you solution will work, but I am worried it will break in future updates when the HTML structure changes. (It is changing quite significantly in a future major update). I do however like your idea for a filter around the cache so I will add that in

    Also, let me know if you think #2 above will solve this issue

    Thanks for the reply!

    I would hate to see a performance penalty for choosing random. While my current example is a temporary hack, the approach is good per se and benefits from the cache.

    Consider changing from an HTML cache to a “gallery cache”, keeping all the info in some structured format (JSON is trivial to do in PHP). Generate the HTML from that when it is retrieved.

    This would be as fast as the current method, could easily support random order by the plugin, provide a useful filter point and improve site portability.

    On that last point, I have a setup where I move sites between development and production. I had to extend it to delete foogallery_cache metakeys from postmeta (which of course is then recreated) in order for it to work automatically. The “gallery cache” approach could fix this small issue too.

    Thread Starter wpress2010

    (@wpress2010)

    Nice sleuthing, rebel.ga – not something I am capable of doing.

    I guess I will just wait for the update, as I tend to shy away from patches that might break when the inevitable update is issued. It makes managing various sites too prone to forgetting about custom patches that may or may not have been done.

    Thanks wpress2010. Patching is problematic unless you fork or have some other means of handling it.

    In this case, which is a hack and could stop working for a variety of reasons, loss of the patch would just stop randomizing the gallery until the patch is reapplied.

    For my own environment, I wrote something I call autopatch which reapplies any patches automatically when plugins are updated (and notifies me if there are any issues). I use this only when there is no other supported means of accomplishing the same thing. Even then, I try to limit my patches to additional hooks wherever possible (as in this case).

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Gallery random order sort/thumbnail: Album?’ is closed to new replies.