Adding a search bar to the NextGEN Gallery Plugin
This code allows you to be able to search for character names in the search bar, and for the engine to find it in the tag, titles, and descriptions of NextGen images and posts.
In the folder for the theme you are using, look for search.php and functions.php. If you don’t have a search.php, you may use the index.php or you can make a search.php by copying the index.php.
In your search.php file, insert the following code at the place where you want the pictures to appear.
[php]
<?php
// Start of NextGen Gallery search
if(is_search()) {
$search = $wp_query->get(‘s’);
$keywords = preg_replace(‘/\+/’,’ ‘,$search);
if (function_exists (‘ngg_get_search_pictures’)) { // function from functions.php
$nggpictures = ngg_get_search_pictures($keywords, ”); // put the number of pictures by row you want, if you don’t want "4"
echo "<h2>Pictures</h2>";
if ($nggpictures) {
echo $nggpictures;
echo ‘<div class="clear">&nbsp;</div>’;
}
else {
echo ‘<p>No pictures were found.</p>’;
}
}
}
// End of NextGen Gallery search
?>
[/php]
In my search.php, I put it outside the “if (have_posts())” section because I wanted to search pictures whether or not there were posts found.
Add the following code to the end of functions.php.
[php]
<?php
## Function to do search on gallery pics from NextGen Gallery plugin
##
## 2 vars : (1) $keywords (usually coming from the standard search query from wordpress)
## (2) $numberPicCol (number of pic by row, if null it takes 4 )
function ngg_get_search_pictures ($keywords, $numberPicRow = NULL) {
global $wpdb;
// $count=1;
// if (!$numberPicRow) { $numberPicRow = "4"; }
$nngquery = "
SELECT pid,description,alttext
FROM wp_ngg_pictures
WHERE MATCH (description, filename, alttext) AGAINST (‘$keywords’ IN BOOLEAN MODE)
AND exclude = ’0′
## start of tags code
UNION
SELECT pid,wp_ngg_pictures.description,alttext
FROM wp_ngg_pictures, wp_terms, wp_term_taxonomy, wp_term_relationships
WHERE wp_terms.term_id = wp_term_taxonomy.term_id and
wp_term_taxonomy.taxonomy = ‘ngg_tag’ and
wp_term_taxonomy.term_taxonomy_id = wp_term_relationships.term_taxonomy_id and
wp_term_relationships.object_id = wp_ngg_pictures.pid and
MATCH (wp_terms.name) AGAINST (‘$keywords*’ IN BOOLEAN MODE)
AND exclude = ’0′
## end of tags code
";
$pictures = $wpdb->get_results($nngquery, ARRAY_A);
if ($pictures) foreach($pictures as $pic) {
$out .= ‘<div class="ngg-gallery-thumbnail">’;
$out .= ‘<a href="’.nggGallery::get_image_url($pic[pid]).’" title="’.stripslashes($pic[description]).’" class="thickbox" rel="singlepic’.$pic[pid].’">’;
$out .= ‘<img src="’.nggGallery::get_thumbnail_url($pic[pid]).’" alt="’.stripslashes($pic[alttext]).’" title="’.stripslashes($pic[alttext]).’" />’;
$out .= "</a></div>\n";
// pictures use float left, so don’t need the code that outputs a <br />
// if ($count == 0) {
// $out .= "<br />";
// }
// ++$count;
// $count%=$numberPicRow;
}
return $out;
};
?>
[/php]
This code searches a picture’s file name, description, alternate text, and tags. I require an exact match for the first three because I was getting too many false positives using an inexact match.
The tag search looks for tags that start with the keyword. You can easily change the exact/inexact match behavior to suit yourself: In the AGAINST phrase, use ‘$keywords’ to require an exact match and ‘$keywords*’ for an inexact match. The first part of the SELECT searches file name, description, and alternate text, and the second part searches the tags.
This search method does have some peculiarities due to MySQL limitations: It will not find words that have less than 4 characters or are on this list. If you want to use short tags or ones on the list, I suggest adding enough characters to the end of the tag to remove the problem, then using an inexact search to find it.
Remember that your visitors cannot see the actual tag on the picture. In one of the discussion threads, a person was having problems with the tag “elk”; this is why. He could get around the problem by using “elkk” or “elk_” or “elk2″ or any number of other variations on “elk”.
.via { WordPress Forums from user nancyb7 }

Comment by kamal — December 28, 2009 @ 3:20 pm
Thanks for this code, but there are a problem with php syntax highlighter!!!
Comment by Alison Shuman — July 30, 2010 @ 9:07 pm
Can you show me how to filter out duplicate images?
For each type of granite on this site, I typically upload pix into two galleries: one for the rooms in the house (“kitchen”, “bathroom”) and one for the project.
This means, if I search on something like “typhoon bordeaux”, I get duplicate photos.
How can I handle this?
Thank you SO much for posting this. It took me a while to figure the code out but when it started working, I was hoping up and down with glee!
Comment by Some Guy — August 12, 2010 @ 2:10 pm
Is there a way to narrow the search to only certain galleries or albums?
Comment by minipower — February 10, 2011 @ 1:59 pm
Hi, i tried to paste the code above and then i figured out that i had to translate them in tags, after done that i receive this error “Parse error: syntax error, unexpected ‘;’, expecting T_PAAMAYIM_NEKUDOTAYIM in functions.php on line 12″, can you help me? the code that i added on the page functions.php is the following:
## Function to do search on gallery pics from NextGen Gallery plugin
##
## 2 vars : (1) $keywords (usually coming from the standard search query from wordpress)
## (2) $numberPicCol (number of pic by row, if null it takes 4 )
function ngg_get_search_pictures ($keywords, $numberPicRow = NULL) {
global $wpdb;
// $count=1;
// if (!$numberPicRow) { $numberPicRow = “4″; }
$nngquery = ”
SELECT pid,description,alttext
FROM wp_ngg_pictures
WHERE MATCH (description, filename, alttext) AGAINST (‘$keywords’ IN BOOLEAN MODE)
AND exclude = ’0′
## start of tags code
UNION
SELECT pid,wp_ngg_pictures.description,alttext
FROM wp_ngg_pictures, wp_terms, wp_term_taxonomy, wp_term_relationships
WHERE wp_terms.term_id = wp_term_taxonomy.term_id and
wp_term_taxonomy.taxonomy = ‘ngg_tag’ and
wp_term_taxonomy.term_taxonomy_id = wp_term_relationships.term_taxonomy_id and
wp_term_relationships.object_id = wp_ngg_pictures.pid and
MATCH (wp_terms.name) AGAINST (‘$keywords*’ IN BOOLEAN MODE)
AND exclude = ’0′
## end of tags code
“;
$pictures = $wpdb->get_results($nngquery, ARRAY_A);
if ($pictures) foreach($pictures as $pic) {
$out .= ”;
$out .= ‘‘;
$out .= ”;
$out .= “\n”;
// pictures use float left, so don’t need the code that outputs a
// if ($count == 0) {
// $out .= “”;
// }
// ++$count;
// $count%=$numberPicRow;
}
return $out;
};
Thx in advance
Comment by brandon — July 24, 2011 @ 4:27 pm
Is there anyway I can get this to display the image and the description beneath as it would in the gallery view?