Working with search recognition in WordPress
May 17th, 2007 @ 21:28I'm currently working on my master project at the Danish School of Journalism where I'm doing a communications assignment for a café and nightclub in Copenhagen named Nadsat.
A part of my project is to create a new website for Nadsat which I have chosen to be, of course, powered by WordPress.
As I've included a search that also searches in pages in WordPress (by using Dan Cameron's Search Everything plugin) since WordPress doesn't do this by default, I got an idea:
Why not have some sort of recognition to save people to some time when they're searching for information on the site? Let's say that someone searches Nadsat.dk for "effekten", which is a collective throwing parties at Nadsat, why not save them time by telling them where they can read more about Effekten (Danish for "the effect")?
So here's what you do. You take the code provided below and use it somewhere in you Search Results page (found in WordPress admin ==> Presentation ==> Theme Editor) and you're set :-)
Okay, so here we go. Remember that "$s" is the search query and we'll be using this in this classic elseif PHP code. Also note that if you want to write a quotation mark in PHP you write \". Let's get to the code:
<? if ($s == "x") {
echo "You searched for x. Blah blah blah <a href=\"http://www.test.com/test\">click this link</a>";
} elseif ($s == "y") {
echo "You searched for y. Blah blah blah and so on";
} elseif ($s == "z") {
echo "You searched for z and probably get it by now";
} else {
echo "Your search query \"" . $s . "\" wasn't recognized";
} ?>
This is just basic PHP, no WordPress-only in there :)
*** UPDATE UPDATE UPDATE ***
I just realized that the code above is case sensitive. With the code below a search for "Effekten" will be converted to "effekten" as would "effeKten", "eFfEkTeN" and… you get the picture :-)
<? if (strtolower($s) == "x") {
echo "You searched for x. Blah blah blah <a href=\"http://www.test.com/test\">click this link</a>";
} elseif (strtolower($s) == "y") {
echo "You searched for y. Blah blah blah and so on";
} elseif (strtolower($s) == "z") {
echo "You searched for z and probably get it by now";
} else {
echo "Your search query \"" . $s . "\" wasn't recognized";
} ?>
Tags: CMS, PHP, Search, Searches, wordpress