Documentation

Light-weight, fast, simple and powerful!
 
Translations of this page?:

Site Search for Wolf CMS

Introduction

This article describes two methods for adding searching to a Wolf CMS site: the first a “native” search method using a PHP script; the second using Google's search service.

Plugin-based Search

Wolf users interested in site search should check out the Site Search plugin provided by user tkeil69575.

Snippet-based Search

This approach has been extensively discussed in the forum; full credit to Wolf user/forum member BlueWolf who devised and developed this solution. Go to the TestWolf site if you would like to see this site search in action (use the form in the right sidebar).

There are three steps involved, each outlined below. Some may require tweaking (either of code or CSS) for use on individual sites. Names for snippet/page have been recommended; you can alter them if you wish.

1. Prepare Snippet

Create a new snippet, the name used here is sitesearch. Copy/paste in the code below, leaving the text filter as -none-:

<?php
function is_in_array($str, $array) {
    return preg_grep('/^' . preg_quote($str, '/') . '$/i', $array);
}
function snippet_sitesearch($parent){
    $exclude = array("search-results");
    $mysearch = htmlentities($_POST['search']);
        $childs = $parent->children();
        if (count($childs) > 0){        
            foreach ($childs as $child) {
                if(!in_array($child->parent()->slug(),$exclude)){        
                    $myparent = snippet_sitesearch($child);
                    $mydate = $child->date('%e %b %Y', 'published');
                    $mylink = $child->link();
                    $url_segments = explode('/', $child->uri());
                    $url_seg_count = count($url_segments);
                    if($url_seg_count > '1'){
                        $mycrumb = ucfirst(str_replace('-',' ',$child->parent()->breadcrumb())).' / '.$child->breadcrumb();
                    } else {
                        $mycrumb = $child->breadcrumb();
                     }
                    if(stristr($mychild.$child->content(),$mysearch) || stristr($mychild.$child->title(),$mysearch) || is_in_array($mysearch,$child->tags())){
                        $out .= '<li><strong><a href="'.$child->uri().URL_SUFFIX.'">'.$mycrumb.'</a></strong> <span style="color:#999;">[published: '.$mydate.']</span></li>';
                    }
                    $out .= snippet_sitesearch($child);
                }
            }
        }
        $out = str_replace("<li><li>", "<li>", $out);
        $out = str_replace("</li></li>", "</li>", $out);
        return $out;
}
?>
<div id="listing">
<?php
$searchdata = "";
$searchdata .= snippet_sitesearch($this->find('/'));
$occurrences = substr_count($searchdata,"<li>");
if($searchdata != NULL){
    if($occurrences > '1'){$results="pages";}else{$results="page";}
    echo "<p>Your search term, <strong>" . $_POST['search'] . "</strong>, was found on the following $results:</p>";
    echo "<ol>";
    echo $searchdata;
    echo "</ol>";
} else {
    echo "<p>Search result for: <strong>" . $_POST['search'] . "</strong></p>";
    echo "<p>No matching results were found.</p>";
}
?>
</div>

Save your snippet.

2. Set up Results page

Create a new hidden page as a child of Homepage, called “Search Results”. Leave the text filter setting at -none-, and copy/paste in the following code:

<?php
if (isset($_POST['search'])){    
    $searchcriteria = $_POST['search'];
    if($searchcriteria != NULL && preg_match('#^[a-z0-9\x20\-]+$#i', $searchcriteria)){
        $this->includeSnippet('sitesearch');
    }else{
        echo "<p>Please input a search criteria.</p>";
    }
}
?>

Save your page.

3. Add search form

In your Layout, add the search form at a convenient location (sidebar or header are typical):

<div class="search-form">
<form action="/search-results<?php echo URL_SUFFIX;?>" method="post">
<input type="text" name="search" class="text" value="<?php echo strip_tags($_POST['search']);?>" />
<input type="submit" name="searchsubmit" value="Search" class="button" /></form>
</div>

Some of these values might require adjustment. In particular, you might wish to use your own CLASS name for styling the DIV.

You should now test your search form: everything should be working!

Integrating Google Custom Search Engine (CSE)

Google offers a helpful free1) Custom Search service. There is an example available which allows you to search the Wolf information found on the personal sites of the Wolf team.

To use this service you must have a Google account. It involves two simple steps:

1. Fill out the form giving this CSE a name, description, and sites to be searched. This last item makes it especially useful for those who wish to search more than one Wolf site from a single search form.

2. You will then be offered a screen which allows you to test your search form and select a style for it (along with some basic customization options to integrate it into the look of your site).

Once you are satisfied, you will be given the code which you can paste into your Wolf site.

1) The free service includes ads; one can opt for the paid-for version which removes ads and offers greater customization.
 
tutorials/site_search.txt · Last modified: 2013-08-08 17:22 by David
 
Except where otherwise noted, content on this wiki is licensed under the following license:GNU Free Documentation License 1.2
Copyright 2010 wolfcms.org / design by yello studio / Wolf CMS Inside