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.

2-Week 30-40% OFF Promo

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.

// 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"> </div>’;
else {
echo ‘<p>No pictures were found.</p>’;
// End of NextGen Gallery search

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.

## 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
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 = and
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;

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 }

19,612 Website Templates and Themes From $3