Support » Plugins » [Plugin: NextGEN Gallery] Adding a Search Bar

Viewing 15 replies - 1 through 15 (of 34 total)
  • 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 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 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. 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.

    Here is the code that goes into the search.php file:

    // 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:

    ## 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
    				MATCH ( 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

    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!


    ok – I found it. now it works.


    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?

    Hi gasjeerbij,
    I think you would have to modify your keyword string to put “+” in front of both words. See

    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”…

    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;

    thanks alot, thats what i wanted!

    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!

    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.

    Nevermind guys, I got it.

    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?

    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?

    Edit: “my only result…”.

    Word Nerd.

Viewing 15 replies - 1 through 15 (of 34 total)
  • The topic ‘[Plugin: NextGEN Gallery] Adding a Search Bar’ is closed to new replies.