brandnewbox.co.uk

Home of Andrew Weaver, a uk-based web developer specialised in building high-quality websites

Setting up an ExpressionEngine Opensearch plugin

Monday, January 29th, 2007

Andrew Weaver

Category:

Providing Opensearch capability on your website allows your site’s search facility to accessed directly from FireFox’s or IE7’s search box.

This article describes how to add an Opensearch function to your Expression Engine site using EE’s built-in search templates.

The Problem

Although not essential to the Opensearch syntax, IE7 is unable to handle searches that send their queries as POST requests. This is, unfortunately, how EE searches work.

The pMachine Knowledge Base provides a cunning AutoSearch answer, but as it requires Javascript will not always be a suitable solution.

The Autosearch plugin was developed to work around this problem. By creating an extra template and pointing the plugin to it, it is possible to send search requests on the URL (ie, as a GET request).

Setting Up

Firstly, download and install the autosearch plugin.

The system requires 3 templates.

The search results template

Your existing search results page will do for this. I’ll call this template search/results

The search box template

This should contain the code to generate the search box and ideally NOTHING ELSE. Don’t put any headers or text in this template - this template will never be seen by the user and is just used to set up the search. I’ll call this search/searchbox

{assign_variable:my_weblog="default_site"}
{assign_variable:my_template_group="site"}
{exp:search:simple_form weblog="{my_weblog}" result_page="search/results"}
<label for="keywords">Search:</label>
<input type="text" class="text" name="keywords" id="keywords" value="" size="18" maxlength="100" />
<input type="submit" class="button" value="Search" />
{/exp:search:simple_form}

The opensearch template

This is the template that initiates the search and should contain the call to the autosearch plugin.

{ exp:autosearch 
        url="http://www.brandnewbox.co.uk/search/searchbox" 
        keywords="{segment_3}" }

Again, the user will never see this page so don’t bother adding headers or footers.

Call this template (something like) search/opensearch

Testing the search works

Call the search from your browser’s address bar by calling the opensearch template followed by the search term, eg, http://www.your-site.com/index.php/search/opensearch/SearchTerm

Your search results page should be returned.

Autodiscovery of your Opensearch

First you need an Opensearch description file. I’ve saved this:

<OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/" xmlns:moz="http://www.mozilla.org/2006/browser/search/">
	<ShortName>brandnewbox.co.uk</ShortName>
	<Description>Search brandnewbox.co.uk</Description>
	<InputEncoding>utf-8</InputEncoding>
	<Url type="application/rss+xml" template="http://www.brandnewbox.co.uk/search/opensearch/{searchTerms}"/>
	<Url type="text/html" template="http://www.brandnewbox.co.uk/search/opensearch/{searchTerms}"/>
	<moz:SearchForm>http://www.brandnewbox.co.uk/search/
</OpenSearchDescription>

to:

http://www.brandnewbox.co.uk/opensearchdescription.xml

Then in your web pages’ header add:

<link rel=
"search" type="application/opensearchdescription+xml" title=
"Search brandnewbox.co.uk" href="/opensearchdescription.xml">

Then, on viewing your website in FF2 or IE7 you should see your Opensearch appear in the search box menu.

Comments

1. David Webb
12th Mar 2007 at 8:23 am

Hey Andrew, Just trying to implement this and most of it works. except it doesn’t seem to be working correctly when it displays the results.  It looks like its trying to show the correct page but all the < head > info is missing. Any ideas on why this would be happening?

Add a comment

 
Remember my personal information
 
Notify me of follow-up comments?
 

© brandnewbox.co.uk 2004-2008