Title: [Plugin: NextGEN Gallery] Adding a Search Bar
Last modified: August 19, 2016

---

# [Plugin: NextGEN Gallery] Adding a Search Bar

 *  [fyuvix](https://wordpress.org/support/users/fyuvix/)
 * (@fyuvix)
 * [16 years, 8 months ago](https://wordpress.org/support/topic/plugin-nextgen-gallery-adding-a-search-bar/)
 * Hello, I’ve found lots of variants on how people are adding the search capability
   to NextGen gallery but I’ve tried the coding and wound up with a syntax error.
   Here’s some other links I’ve found relating to the search capabilities
    [http://www.ikiru.ch/blog/2008/how-to-add-a-search-function-for-nextgen-gallery-wordpress-plugin](http://www.ikiru.ch/blog/2008/how-to-add-a-search-function-for-nextgen-gallery-wordpress-plugin)
   And it’s an ongoing topic here [http://wordpress.org/support/topic/230905?replies=1#post-944495](http://wordpress.org/support/topic/230905?replies=1#post-944495)
 * But I was just wondering if anyone has a single solid working piece of code for
   the latest 1.3 version of NextGen. I just want people to be able to search for
   character names in the search bar, and for the engine to find it in the TAGS,
   titles, and descriptions of NextGen images, comic pages, and posts. Please help!

Viewing 15 replies - 1 through 15 (of 34 total)

1 [2](https://wordpress.org/support/topic/plugin-nextgen-gallery-adding-a-search-bar/page/2/?output_format=md)
[3](https://wordpress.org/support/topic/plugin-nextgen-gallery-adding-a-search-bar/page/3/?output_format=md)
[→](https://wordpress.org/support/topic/plugin-nextgen-gallery-adding-a-search-bar/page/2/?output_format=md)

 *  [nancyb7](https://wordpress.org/support/users/nancyb7/)
 * (@nancyb7)
 * [16 years, 8 months ago](https://wordpress.org/support/topic/plugin-nextgen-gallery-adding-a-search-bar/#post-1189496)
 * I’ve been working really hard on this the last week or so, and I have code that
   works, as well as possible using a MySQL full-text search. I have relied heavily
   on the links given above, as well as others that they lead to.
 * I am using NextGen Gallery 1.3.5 and WordPress 2.8.4. My shared server provides
   PHP 4.3.11 and MySQL 4.1.14.
 * 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 [this code](http://realitycheck.cbftimes.com/reference/nggsearchcall.txt)
   at the place where you want the pictures to appear. In my search.php, I put it
   outside the “<?php if (have_posts()) : ?>” section because I wanted to search
   pictures whether or not there were posts found.
 * Add [this code](http://realitycheck.cbftimes.com/reference/nggsearchfunction.txt)
   to the end of functions.php. The code I found through other discussions was written
   for versions of NextGen Gallery prior to 1.0, or was poorly adapted for 1.0 and
   did not work correctly.
 * My 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](http://dev.mysql.com/doc/refman/4.1/en/fulltext-stopwords.html).
   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”.
 * Since the WordPress post search does not have these problems, I assume they use
   a different search method, but I do not know MySQL or WordPress well enough to
   figure out what it might be.
 *  [nancyb7](https://wordpress.org/support/users/nancyb7/)
 * (@nancyb7)
 * [16 years, 8 months ago](https://wordpress.org/support/topic/plugin-nextgen-gallery-adding-a-search-bar/#post-1189497)
 * Here is the code that goes into the search.php file:
 *     ```
       <?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
       ?>
       ```
   
 * And here is the code to add to the functions.php file:
 *     ```
       <?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;
       };
       ?>
       ```
   
 * If you don’t want the description, file name, and alternate text to all be searched,
   just remove the appropriate field(s) from
    `WHERE MATCH (description, filename,
   alttext) AGAINST ('$keywords' IN BOOLEAN MODE)`
 * I commented out some code that I copied from Psykotik’s post because I didn’t
   need it, but didn’t want to lose the way the code had originally been written.
 * To search tags on posts we use this plugin [https://redmine.sproutventure.com/projects/show/search-everything](https://redmine.sproutventure.com/projects/show/search-everything).
 *  [maggymae](https://wordpress.org/support/users/maggymae/)
 * (@maggymae)
 * [16 years, 8 months ago](https://wordpress.org/support/topic/plugin-nextgen-gallery-adding-a-search-bar/#post-1189498)
 * hi nancyb7,
 * that’s what I’m looking for!!!
    But I don’t know what I’m doing wrong – it doesn’t
   work in my Gallery.
 * Could you look and maybe tell me whats wrong??!!!
 * thanks in advance!
 * maggy
    [http://www.oesterreichbilder.at](http://www.oesterreichbilder.at)
 *  [maggymae](https://wordpress.org/support/users/maggymae/)
 * (@maggymae)
 * [16 years, 8 months ago](https://wordpress.org/support/topic/plugin-nextgen-gallery-adding-a-search-bar/#post-1189499)
 * ok – I found it. now it works.
 * maggy
 *  [gasjeerbij](https://wordpress.org/support/users/gasjeerbij/)
 * (@gasjeerbij)
 * [16 years, 8 months ago](https://wordpress.org/support/topic/plugin-nextgen-gallery-adding-a-search-bar/#post-1189502)
 * isn’t it possible to get the admin search function working on the frontend of
   the website? That search function works perfect.
 * I am using almost the same code as above for my site some time now.
    But if people
   for example search on NEXTGEN GALLERY, they get both the results for the word
   NEXTGEN and GALLERY. And i know u can use “, but most of the people browsing 
   the internet don’t know that. So what i want is that the results show only pictures
   that match both NEXTGEN and GALLERY.
 * Maybe someone has a solution to this?
 *  [nancyb7](https://wordpress.org/support/users/nancyb7/)
 * (@nancyb7)
 * [16 years, 8 months ago](https://wordpress.org/support/topic/plugin-nextgen-gallery-adding-a-search-bar/#post-1189503)
 * Hi gasjeerbij,
    I think you would have to modify your keyword string to put “
   +” in front of both words. See [http://dev.mysql.com/doc/refman/4.1/en/fulltext-boolean.html](http://dev.mysql.com/doc/refman/4.1/en/fulltext-boolean.html).
 *  [gasjeerbij](https://wordpress.org/support/users/gasjeerbij/)
 * (@gasjeerbij)
 * [16 years, 8 months ago](https://wordpress.org/support/topic/plugin-nextgen-gallery-adding-a-search-bar/#post-1189504)
 * Hi nancyb7, i allready saw that page, but it didn’t really help me since im not
   really an expert in mysql.
 * What i basicly want is a phrase search without the user have to put te text manualy
   in “example text”…
 *  [nancyb7](https://wordpress.org/support/users/nancyb7/)
 * (@nancyb7)
 * [16 years, 8 months ago](https://wordpress.org/support/topic/plugin-nextgen-gallery-adding-a-search-bar/#post-1189505)
 * gasjeerbij,
    Actually, you use PHP to modify the keyword string, and the code
   to put a “+” in front of each word could get rather involved. You’d have to break
   the string into words, put the “+” in front of each word, then put them back 
   into one string. But if you just want to make it a phrase search, try `$keywords
   = '"' . $keywords . '"';` near the beginning of the function, any place before
   the `$nngquery = "` line. Note: I have not tested this code.
 * In the code that I copied from Psykotik’s post, the code that you put into search.
   php removes \, +, and / from the keyword string by replacing them with blanks.
   I’m not sure of the reason for this and left it in. You could try modifying this
   line to allow the visitor more control over their search. The line is
    `$keywords
   = preg_replace('/\+/',' ',$search);` You cannot take it out completely. If you
   want to eliminate all the character changes, replace the line with this one `
   $keywords = $search;`
 *  [gasjeerbij](https://wordpress.org/support/users/gasjeerbij/)
 * (@gasjeerbij)
 * [16 years, 8 months ago](https://wordpress.org/support/topic/plugin-nextgen-gallery-adding-a-search-bar/#post-1189506)
 * thanks alot, thats what i wanted!
 *  [site2shan](https://wordpress.org/support/users/site2shan/)
 * (@site2shan)
 * [16 years, 8 months ago](https://wordpress.org/support/topic/plugin-nextgen-gallery-adding-a-search-bar/#post-1189508)
 * Does anyone know how we can get this to return a result of galleries, instead
   of single images? We would like the search function to search the gallery description
   and then return a result of links to galleries that fall within the search parameters.
   Thanks for any help!
 *  [bgardnerniermannweekscom](https://wordpress.org/support/users/bgardnerniermannweekscom/)
 * (@bgardnerniermannweekscom)
 * [16 years, 7 months ago](https://wordpress.org/support/topic/plugin-nextgen-gallery-adding-a-search-bar/#post-1189510)
 * Thanks for the great code for my search feature.
 * My question is why the code is overriding my theme’s color palette and resetting
   them to the theme default colors?
 * I’m very inexperienced with coding, and really need specific help to overcome
   this problem. Thanks so much.
 *  [bgardnerniermannweekscom](https://wordpress.org/support/users/bgardnerniermannweekscom/)
 * (@bgardnerniermannweekscom)
 * [16 years, 7 months ago](https://wordpress.org/support/topic/plugin-nextgen-gallery-adding-a-search-bar/#post-1189511)
 * Nevermind guys, I got it.
 *  [Steve S](https://wordpress.org/support/users/steve-s/)
 * (@steve-s)
 * [16 years, 7 months ago](https://wordpress.org/support/topic/plugin-nextgen-gallery-adding-a-search-bar/#post-1189513)
 * I have my image search working, but I need to limit the search to a few specific
   galleries. Does anyone have a way to do this?
 * I need to either specify which galleries to search, or maybe even add a specific
   keyword to all the images in those galleries, and then invisibly add that keyword
   to the user’s search term.
 * Any ideas?
 *  [FotoAndy](https://wordpress.org/support/users/fotoandy/)
 * (@fotoandy)
 * [16 years, 7 months ago](https://wordpress.org/support/topic/plugin-nextgen-gallery-adding-a-search-bar/#post-1189515)
 * Thanks to everyone who’s worked this far to get this searching to play nice with
   WordPress search.
 * I’ve implemented the code and run a search (both wih and without quoted), and
   my only reselt is an empty page with the text:
 * > no database caching for now!
 * Any thoughts? Do I simply need to wait for the cache to build up?
 *  [FotoAndy](https://wordpress.org/support/users/fotoandy/)
 * (@fotoandy)
 * [16 years, 7 months ago](https://wordpress.org/support/topic/plugin-nextgen-gallery-adding-a-search-bar/#post-1189516)
 * Edit: “my only result…”.
 * Word Nerd.

Viewing 15 replies - 1 through 15 (of 34 total)

1 [2](https://wordpress.org/support/topic/plugin-nextgen-gallery-adding-a-search-bar/page/2/?output_format=md)
[3](https://wordpress.org/support/topic/plugin-nextgen-gallery-adding-a-search-bar/page/3/?output_format=md)
[→](https://wordpress.org/support/topic/plugin-nextgen-gallery-adding-a-search-bar/page/2/?output_format=md)

The topic ‘[Plugin: NextGEN Gallery] Adding a Search Bar’ is closed to new replies.

 * 34 replies
 * 17 participants
 * Last reply from: [guruscotty](https://wordpress.org/support/users/guruscotty/)
 * Last activity: [15 years, 9 months ago](https://wordpress.org/support/topic/plugin-nextgen-gallery-adding-a-search-bar/page/3/#post-1189593)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
