The Photo Gallery by WD – Responsive Photo Gallery plugin is great for building small galleries to display on a blog, and providing a basic search tool to filter using the descriptions and titles of images. For larger galleries however, the search tool is limited, such that it only returns exact in-string matches, meaning that the order of search terms must exactly match the order of words in the description. Recently I created a large gallery of 600 images for wedding photos and wanted friends and family to be able to search for themselves by name or by activity, for example searching for “centrepiece venue” would return all the images that matched either word in any order, even if the description of the image was “venue centrepiece”.
Altering The WDGLibrary.php File
The first file to alter is “framework/WDGLibrary.php
“, which contains the get_image_rows_data
function. This is the function which retrieves the thumbnail images based on the general gallery widget settings, but also factors in to account any search terms. The WHERE clause for the search terms is under the following predicate at line 1200:
if ( $bwg_search ) { $where = 'AND (image.alt LIKE "%%' . $bwg_search . '%%" OR image.description LIKE "%%' . $bwg_search . '%%")'; }
This means that any search string must exactly match a string inside either the image alternative name (image.alt
) or the image description (image.description
). What we want to do here is split the search string in to an array separated by spaces, then add each individual word to the where clause.
if ( $bwg_search ) { $where = 'AND (('; $bwg_search_array = explode(' ', $bwg_search); foreach($bwg_search_array as $bwg_search){ $where .='image.alt LIKE "%%' . $bwg_search . '%%" AND '; } $where = substr($where, 0, -4) . ') OR ('; foreach($bwg_search_array as $bwg_search){ $where .='image.description LIKE "%%' . $bwg_search . '%%" AND '; } $where = substr($where, 0, -4) . '))'; }
Here we first create an array by exploding the search string, then run two “foreach
” loops to create clauses for both the alt name and description. There’s also a little tidying up of the strings so we don’t end up with rogue “AND
” keywords. Once this change was made, the results came back as expected (before and after images below):
There was one problem though, when clicking any image returned with this new query, the lightbox that appeared showed the error “The image has been deleted.“, despite the link working.
Altering The BWGModelGalleryBox.php File
In order to fix the above problem, there is another near-identical query in the “frontend/models/BWGModelGalleryBox.php
” file which is used to generate the lightbox at line 27:
if ($filter_search_name != '') { $where = ' AND (image.alt LIKE "%%' . $filter_search_name . '%%" OR image.description LIKE "%%' . $filter_search_name . '%%")'; }
The change required is near identical to the previous change, just with some variable names changed to keep the same standard of the code area:
if ($filter_search_name != '') { $where = ' AND (('; $filter_search_name_array = explode(' ', $filter_search_name); foreach($filter_search_name_array as $filter_search_name_1){ $where .='image.alt LIKE "%%' . $filter_search_name_1 . '%%" AND '; } $where = substr($where, 0, -4) . ') OR ('; foreach($filter_search_name_array as $filter_search_name_2){ $where .='image.description LIKE "%%' . $filter_search_name_2 . '%%" AND '; } $where = substr($where, 0, -4) . '))'; }
That change resolved the issue with the lightbox, and now the images would load as expected.