Blog » Site-Wide Search Now Available

Site-Wide Search Now Available

This website now features a search function which can be found in the left column. I have opted to use Google custom search since I have found that Google's search engine often is better at locating information than a website's own search engine. Google's search technology is simply far superior to many of the other search engines provided with various Content Management Systems (CMS). Adding search functionality was relatively easy, although it did require that I create a custom page type for the search results page. The reason for using a custom page type is because the HTML insert functionality in the Silverstripe CMS won't embed the Google search results page code properly; it performs some sort of operation after closing the HTML edit window that destroys it.

Creating the custom search results page can be done as follows:

  • Create the file mysite/code/SearchResultsPage.php containing the following:

class SearchResultsPage extends SiteTree {
static $db = array(
static $has_one = array(

class SearchResultsPage_Controller extends ContentController {
function init() {


  • Create a new style file called themes/<mytheme>/templates/Layout/ (replace <mytheme> with the directory containing the theme that your site is using)

  • should contain a modified version of your site template that does not contain any other Google Adsense advertising (assuming that the code was obtained from the Google Adsense for Search service) since this is against the terms of service. It should also contain the Google custom search results code, e.g.:


<% base_tag %>
<link rel="shortcut icon" href="/favicon.ico" />
<script type="text/javascript"
<script type="text/javascript"

<div id="wrap">
<div id="top-bg"></div>
<div id="header">
<h1 id="logo-text">
a href="index.html" title="">Logo<span>Text</span></a></h1>
<p id="slogan">Slogan &amp; here</p>

<div id="header-links">
<img src="themes/custom/images/header-small.jpg" width="398"
height="60" alt="header photo small"/>
<div id="nav">
<% include Navigation %>

<div id="content-wrap">
<div id="search">
<div class="typography">
<a name="TemplateInfo"></a>



<div id="footer-wrap">
<div id="footer-bottom">
<% include Footer %>
<!-- footer ends-->
<!-- wrap ends here -->
  •  Assuming that the search entry form should appear on every page, the search form code will need to be inserted into one of the main template files. I put it into themes/<mytheme>/templates/Includes/,

  • Upload all the new and modified files to the server
  • Flush the cache, i.e., visit http://<mysite>/?flush=1 (replace <mysite> with the URL of your website),
  • Rebuild the database by visiting  http://<mysite>/db/build (replace <mysite> with the URL of your website), and
  • Now log into the CMS, and create the search results page, ensuring that its URL matches the one provided to Google Adsense for Search as the search results URL.

After following these instructions, the search function should be fully operational. If you do not mind showing the results on Google's own website, then these steps are unnecessary. However, having the search results appear within ones own site looks more professional, so it is well worth the effort.

Blog » Site-Wide Search Now Available

Post your comment


No one has commented on this page yet.

RSS feed for comments on this page | RSS feed for all comments

Blog » Site-Wide Search Now Available