Google Custom Search and WordPress

For a more technical turn on this blog, I thought I’d give you a few pointers as to how I managed to get GCS to work on my current project.

First, have you created your search engine yet? Make sure to think thoroughly on which sites you want to add to which search engine, and have a look at the formatting tips for the URLs.

Then, and this is as it is on 01/11/11, go to “Look and Feel” and choose “Results only”:

<div id="cse" style="width: 100%;">Loading</div>
<script src="//www.google.com/jsapi" type="text/javascript"></script>
<script type="text/javascript">
function parseQueryFromUrl () {
var queryParamName = "q";
var search = window.location.search.substr(1);
var parts = search.split('&');
for (var i = 0; i < parts.length; i++) {
var keyvaluepair = parts[i].split('=');
if (decodeURIComponent(keyvaluepair[0]) == queryParamName) {
return decodeURIComponent(keyvaluepair[1].replace(/+/g, ' '));
}
}
return '';
}
var _gaq = _gaq || [];
_gaq.push(["_setAccount", "YOUR_ANALYTICS_ACCOUNT_NUMBER_STARING_WITH_UA"]);
function _trackQuery(control, searcher, query) {
var loc = document.location;
var url = [
loc.pathname,
loc.search,
loc.search ? '&' : '?',
encodeURIComponent('q'),
'=',
encodeURIComponent(query)
];
_gaq.push(["_trackPageview", url.join('')]);
}
google.load('search', '1', {language : 'fr'});
google.setOnLoadCallback(function() {
var customSearchControl = new google.search.CustomSearchControl('YOUR_CODE_FROM_CSE');
customSearchControl.setResultSetSize(google.search.Search.FILTERED_CSE_RESULTSET);
customSearchControl.setSearchStartingCallback(null, _trackQuery);
var options = new google.search.DrawOptions();
options.setAutoComplete(true);
options.enableSearchResultsOnly();
customSearchControl.draw('cse', options);
var queryFromUrl = parseQueryFromUrl();
if (queryFromUrl) {
customSearchControl.execute(queryFromUrl);
}
}, true);
</script>
<link rel="stylesheet" href="//www.google.com/cse/style/look/default.css" type="text/css" />

Then copy this text to a WordPress-Page of your choosing (don’t forget to change the Analytics-number!!), and what matters is the Permalink you give it. yoursite.com/search/ would be nice, but it could also be /google/ or anything else for that matter, just make sure you remember :).

You can at this stage actually already try to see if the Search works by going to your page, in my example it would be yoursite.com/search/ and add a ?q=searchterm to it, giving you yoursite.com/search/?q=searchterm

The idea now would be to create a template specifically for the search results. I will not go into details for this, but if you search for page template wordpress on google, you should find hundreds of tutorials. Remove anything that isn’t useful, and then go back to editing your WordPress-Page and make sure you use the new Template.

And then, a neat feature is to create a file called searchform.php into which you only paste the following code:

<form method="get" id="searchform" action="/search/">
<label for="q" class="assistive-text">Recherche</label>
<input type="text" class="field" name="q" id="q"/>
<input type="submit" class="submit" id="searchsubmit"/ value="Recherche">
</form>

and upload it to your wordpress-template’s main directory. This will be your new Search Widget then, and replace the one that comes with WordPress, and you can put it pretty much anywhere you want on your site where you have widget-areas defined. Play with classes to make it look the way you want, and make sure to change the form’s action to your wordpress-permalink. Not the whole link though, only the bit – as in this example – that comes after yoursite.com.

I hope this works for you guys, it cost me quite a bit of time to figure it all out, whilst actually, it was very simple from the beginning…

Auto-evaluation

Auto-evaluation

Auto-evaluation is work. More work. No matter what anyone will say. Someone has to prepare it, and then execute it. And unless this evaluation is done for the sake of it, it entails more work. If the evaluation states that all is as good as can be, this knowledge...

Eis léiw Assurancen

Eis léiw Assurancen

Virun e puer Wochen, sin ech eng Strooss roofgefuer, wou zum Schluss eppes wei ee klengen Trottoir ass ier een an d'Haaptstrooss oofbeit. Well do awer op der rietser Saeit eng grouss Mauer ass, an op der lenker een deckt Gebesch, sin ech d'eischt bis bei deen Trottoir...

Oh Ubuntu, you are my favorite linux-based OS…

Oh Ubuntu, you are my favorite linux-based OS…

...but boy, your Cloud (Ubuntu One) and Music (Ubuntu One Music & Ubuntu One Music App) connectivity smells! This is a rant, and my first on Ubuntu, so unless you want to blow off some pressure, this won't be your favorite post ;). Whilst the title is a quote...

Representing your organisation is a 24/7 job!

As you're reading this, there is for the purposes of this article only 2 possible situations. You work for a company that does outside representation, or you don't... (work; work for a company; work in a company that provides services or products or both). But whether...

Welcome

Welcome

... to the website of Jean-Claude Bisenius, marketing professional based in Luxembourg. This website will provide you with a snapshot of my educational and professional career, and give you insights into the areas that I take an interest in. Should you wish to contact...

Services

With a rather diversified educational and professional experience, I have come to learn what I am good at, and what others are better at. Thanks to a lot of good guidance over the years, I have come to learn that it is not only best to work with experts, I have come...

Twitter could be (so) useful

Twitter could be (so) useful

As you, loyal reader, might already know, traffic conditions in and around Luxembourg can be pretty bad. We do have radio stations that on a regular basis list a few issues, but they're not always "up-to-speed"... Same goes for those apps that use the public network...

Hornbach: aggressive behaviour, and hardly any service – 1/5 stars

In my recent posts on Batiself, I got into poor customer service of a "home-depot-style" shop. My posts on Cultural Distance touched on the difference between cultures and how they affect perceptions, and my post on Paradigm Shifts tried to highlight a change in...

A societal paradigm shift?

For years, I have been advocating self-checkouts in all kinds of places, yet everyone always told me it wasn't going to catch on. Then back in my London-time, I saw a Tesco in the Bank area that had nearly only self-checkouts and I tried to grasp all the...