<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Digging into WordPress &#187; 2010 &#187; October &#187; 02</title>
	<atom:link href="http://digwp.com/2010/10/02/feed/" rel="self" type="application/rss+xml" />
	<link>http://digwp.com</link>
	<description>Take your WordPress skills to the next level.</description>
	<lastBuildDate>Fri, 17 May 2013 21:10:40 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>Using Google Custom Search in WordPress</title>
		<link>http://digwp.com/2010/10/google-custom-search-in-wordpress/</link>
		<comments>http://digwp.com/2010/10/google-custom-search-in-wordpress/#comments</comments>
		<pubDate>Sat, 02 Oct 2010 17:03:02 +0000</pubDate>
		<dc:creator>Chris Coyier</dc:creator>
				<category><![CDATA[Theme]]></category>
		<category><![CDATA[google]]></category>
		<category><![CDATA[search]]></category>

		<guid isPermaLink="false">http://digwp.com/?p=2342</guid>
		<description><![CDATA[Once a WordPress powered site starts getting quite a bit of content, the default built-in search becomes fairly useless. It just isn&#8217;t very smart. If you wrote a comprehensive article about He-Man, but since have written five other articles that just mentioned He-Man in passing, a search for &#8220;He-Man&#8221; will turn up your comprehensive article [...]]]></description>
				<content:encoded><![CDATA[<p>Once a WordPress powered site starts getting quite a bit of content, the default built-in search becomes fairly useless. It just isn&#8217;t very smart. If you wrote a comprehensive article about He-Man, but since have written five other articles that just mentioned He-Man in passing, a search for &#8220;He-Man&#8221; will turn up your comprehensive article sixth. There have been various tweaks and plugins and whatnot to try and make this better. But why not leverage the best search engine ever written instead? </p>
<p><span id="more-2342"></span></p>
<h3>The Basic Setup</h3>
<p>Sign up for the (free) <a href="http://www.google.com/cse/">Google Custom Search Engine</a>. </p>
<div class="image-wrap">
<img src="http://digwp.com/wp-content/uploads/gcsesignup.png" alt="" title="gcsesignup" width="511" height="383" class="alignnone size-full wp-image-2973" />
</div>
<p>The default code that Google gives you is the pure-JavaScript version. It works fine, but it doesn&#8217;t allow you to separate the search form and the results. I find that kind of ridiculous, as it&#8217;s far more common to want to host the search form on all pages (like in the header or sidebar) and have that take you to a dedicated results page.</p>
<p>To get the code that is the <strong>separate</strong> search form and results, go to the Look and Feel section after creating your CSE. Choose the iframe method, then click the Get Code button. <strong>Update</strong> (2012/09/30): apparently the iframe method is no longer available, so hopefully a workaround using existing methods is still possible.</p>
<div class="image-wrap">
<img src="http://digwp.com/wp-content/uploads/iframecode.png" alt="" title="iframecode" width="442" height="412" class="alignnone size-full wp-image-2974" /></p>
<p><img src="http://digwp.com/wp-content/uploads/separatecode.png" alt="" title="separatecode" width="550" height="603" class="alignnone size-full wp-image-2975" />
</div>
<p>Fill out the URL of your search results page (this will be a page you&#8217;ll create and publish later). Then take the top bit of code and place it were you want the search form to be. Likely in your sidebar.php file or header.php file, wherever your site design dictates would be good to have a search form. </p>
<p>On this site, we put it in the sidebar, and applied our simple button class to match other buttons on the site. Don&#8217;t be afraid to alter the HTML of the provided code as long as it&#8217;s simple alterations like adding a class name to an input.</p>
<div class="image-wrap">
<img src="http://digwp.com/wp-content/uploads/digwpcse.png" alt="" title="digwpcse" width="318" height="77" class="alignnone size-full wp-image-2991" />
</div>
<p>Now create a page in your WordPress admin called &#8220;Search Results&#8221;, in the content, paste in the second bit of code. Make sure the slug you give the page matches the URL you gave the CSE for the location of the results page. If they don&#8217;t match, you can easily change the <code>&lt;form&gt;</code>&#8216;s action attribute in the HTML.</p>
<div class="image-wrap">
<img src="http://digwp.com/wp-content/uploads/matchsearchresultsurl-1.png" alt="" title="matchsearchresultsurl-1" width="512" height="314" class="alignnone size-full wp-image-2977" />
</div>
<p>You&#8217;re all set! Now you can enter search terms on any page of your site, and be taken to a dedicated search results page where the results will be displayed. These results will of course be real Google search results, but limited only to your domain. So that He-Man article? It will almost certainly be on top, because Google&#8217;s importance algorithmic will put it there. </p>
<h3>Using Multiple CSE&#8217;s for Subsections of your Site</h3>
<p>On CSS-Tricks, I have a search area that looks like this:</p>
<div class="image-wrap">
<img src="http://digwp.com/wp-content/uploads/csstrickssearch.png" alt="" title="csstrickssearch" width="384" height="98" class="alignnone size-full wp-image-2992" />
</div>
<p>Notice the little options beneath the search box which indicate in what area you would like to search. It might seem like this is some fancy custom development, but it&#8217;s really not. In reality, it&#8217;s just four different Google Custom Search Engines. Only one of them displays at a time though, depending on which search option is active.</p>
<pre><code>&lt;div id="search-area"&gt;

	&lt;form action="/search-results/" id="search-all" class="header-search-form default-search"&gt;
	    ...
	&lt;/form&gt;

	&lt;form method="get" action="/" id="search-articles" class="header-search-form"&gt;
	    ...
	&lt;/form&gt;

	&lt;form action="/forums/index.php" id="search-forums" class="header-search-form"&gt;
	    ...
	&lt;/form&gt;

	&lt;form action="/snippets/search/" id="search-snippets" class="header-search-form"&gt;
	    ...
	&lt;/form&gt;

	&lt;div id="search-by"&gt;Search: 
		&lt;a href="#search-all" class="cur-search"&gt;All&lt;/a&gt; 
		&lt;a href="#search-articles"&gt;Articles&lt;/a&gt; 
		&lt;a href="#search-forums"&gt;Forums&lt;/a&gt; 
		&lt;a href="#search-snippets"&gt;Snippets&lt;/a&gt;

		&lt;p id="browse-archives"&gt;Or... &lt;a href="/archives/"&gt;browse the archives&lt;/a&gt;&lt;/p&gt;
	&lt;/div&gt;

&lt;/div&gt; &lt;!-- END search-area --&gt;</code></pre>
<p>Notice the class names on the very first <code>&lt;form&gt;</code> includes &#8220;default-search&#8221;. All these forms are hidden by default, and only that class name reveals it. So while there are four forms here, only one displays. Also note the second form actually submits to &#8220;/&#8221; and uses the GET method. This is for &#8220;articles only&#8221; search and uses the default WordPress search. </p>
<p>The other areas (Snippets, Forums, and All) use Google Custom Search Engines. So for those three, I used what is described above in the basic setup. For the Snippets area, I told the CSE to use the domain http://css-tricks.com/snippets/ &#8211; so the results that it returns are limited to that subdirectory.</p>
<p>I use a bit of jQuery to allow the search option links to be clickable to change which area is to be searched. When a new option is selected, hide all forms, show the correct corresponding form, flip around classes to indicate the new option is selected. Easy cheesy.</p>
<pre><code>var $searchByLinks = $("#search-by &gt; a");

$searchByLinks.click(function() {
    var $el = $(this)
    $(".header-search-form").hide();
    $($el.attr("href")).show();
    $searchByLinks.removeClass("cur-search");
    $el.addClass("cur-search");
    return false;
});</code></pre>
<p>Then to keep what users type in all search inputs in sync at all times, another quick bit:</p>
<pre><code>$(".header-search-input").keyup(function() {
    $(".header-search-input").val($(this).val());
});</code></pre>
<p>Now we&#8217;re all set! Users can now search subsections of the site if they choose, and all we had to do was use multiple Google CSE&#8217;s. The user likely doesn&#8217;t even notice and no custom development was needed.</p>
<hr />
<p><small>© 2010 <a href="http://digwp.com">Digging into WordPress</a> | <a href="http://digwp.com/2010/10/google-custom-search-in-wordpress/">Permalink</a> | <a href="http://digwp.com/2010/10/google-custom-search-in-wordpress/#comments">20 comments</a> | Add to <a href="http://del.icio.us/post?url=http://digwp.com/2010/10/google-custom-search-in-wordpress/&title=Using Google Custom Search in WordPress">del.icio.us</a> | Post tags: <a href="http://digwp.com/tag/google/" rel="tag">google</a>, <a href="http://digwp.com/tag/search/" rel="tag">search</a><br/></small></p>]]></content:encoded>
			<wfw:commentRss>http://digwp.com/2010/10/google-custom-search-in-wordpress/feed/</wfw:commentRss>
		<slash:comments>20</slash:comments>
		</item>
	</channel>
</rss>
