Google Custom Search and WordPress

Nov 1, 2011

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…

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...

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...

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...

Cultural distance vs. Paradigm shifts

This week was marked by a development that was somewhat unexpected to me. A place that I've been going to for a very long time now, a place that I've tremendously enjoyed, has been holding my lesser visits against me. Don't get me wrong, the relationship has developed...

Lux-Restaurants: not market-lead?

Over the years, it has come to my attention that Luxembourg has a few obvious issues 🙂 - and the one for today is me going hungry on Monday nights unless I cook myself! Unfortunately, this particular case only applies to my experience in Luxembourg, but maybe some of...

Rules, and the advantages of bending them

When I was a young boy, my parents taught me to be a good boy, to stand up for what I believe in, and to play by the rules. Those rules applied to anyone that wanted to be just a good boy as I was, and ultimately to those that "wanted to go to heaven". These rules...

Marketing presentation at a local cinema, 3/5 stars

The other night, I had been invited to a short notice presentation of a rather new cinema as a channel for our marketing messages. As this coincided with a larger project I am working on, I registered my colleague and myself for the presentation and was already...

Wallstreet Journal has great customer care – 5/5 stars

As some of you might remember, I have in the past already praised the customer service team of the Wallstreet Journal, but they continue to positively surprise me! Since my change of address, the delivery didn't reach me twice (this would be a negative point, I know...