Using DuckDuckGo as a WordPress Search Engine
I mentioned in my Trying Out DuckDuckGo post that I was giving DuckDuckGo a whirl as this sites search engine. I'm happy enough with the result (pardon the pun) that I'm leaving it and thought I'd write a little tutorial for anyone else who wants to use DDG on a wordpress site.
There are a couple ways to do this. One puts a search box on your site, and when someone types a term then hits enter, they're taken to DuckDuckGo's page. The other is the same initially, but instead of bouncing away, DDG's site (with your search results displayed on it) are shown in an iframe on your site. It's the closest I could come to duplicating the Google Custom Search plugin for WordPress. Without further ado…
Style the DuckDuckGo Results:
Here's the search form I'm using right here on DoOpenSource.com, which hits DuckDuckGo remotely.
<form method="get" id="search" action="http://doopensource.com/search-doopensource">
<input type="hidden" name="sites" value="doopensource.com"/>
<input type="hidden" name="k7" value="%23ffffff"/>
<input type="hidden" name="k8" value="%23000000"/>
<input type="hidden" name="k9" value="%2327598a"/>
<input type="hidden" name="kaa" value="%2327598a"/>
<input type="hidden" name="kt" value="a"/>
<input type="text" name="q" maxlength="255" placeholder="Type here to search"/>
<input type="submit" value="DuckDuckGo Search" style="visibility: hidden;" />
All of these hidden inputs are different variables that can be set and altered based on what you'd like to see in the results. For the full list, go to DuckDuckGo's parameters page and use what you need. I'll give you a run down on what I'm doing…
Sites, as you guess, is specifying which site to search (doopensource.com in this case). k7 is the background color (I'm partial to white), k8 is the regular text, k9 is the color of links, and kaa is a the color of visted links.
Most of the colors work fine with three (fff for white, 000 for black) or six (27598a) digit color codes, but some don't. You're best off just preceding them all with a # symbol. This won't quite work either… You'll have to urlencode the #, making it a %23 instead. %2327598a is the same as #27598a.
kt is the font, and in this case I picked a for Arial.
Stick whatever you want in the text input box (Type here to search seemed a safe bet) I opted for no visible button, so the submit is hidden.
Display the DuckDuckGo Results:
As I said earlier, there are a couple of ways to show results that I'm familiar with. One is to just send people off on to DuckDuckGo's website. The other way, and the way I chose, is to show results in an iframe on your own site. Perhaps there's a better way, but I did it thus…
I created a template file called search-page-template.php. The contents of this file are as follows:
Template Name: Search Page
* @package WordPress
* @subpackage DoOpenSource
$kj = $_GET[‘kj’];
$k7 = $_GET[‘k7’];
$k8 = $_GET[‘k8’];
$k9 = $_GET[‘k9’];
$kaa = $_GET[‘kaa’];
$kt = $_GET[‘kt’];
$q = $_GET[‘q’];
<?php print '
<iframe id="ddg" width="100%" onload="resize_iframe()" src="http://duckduckgo.com/?sites=doopensource.com&kj=%23'.$kj.'&k7=%23'.$k7.'&k8=%23'.$k8.'&k9=%23'.$k9.'&kaa='.$kaa.'&kt='.$kt.'&q='.$q.'&ko=-1"></iframe>
$my_id = 9999;
$post_id = get_post($my_id);
$content = $post_id->post_content;
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
</div><!– search-results –>
</div><!– content –>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
$my_id is going to be different in your case. The next step is to make a page that uses Search Page as a template. The page is going to have an id. Look up at the url, and you'll see something like http://yoursite.com/wp-admin/post.php?post=9999&action=edit That post= number is what you stick in the php file you made, 9999 in this case.
That's pretty much it, as far as I can tell. Works for me. When someone writes a plugin that integrates better, I'll start using it. For now, I'm happy with this. DuckDuckGo is indexing my site well (I wish Blekko did too, but it just aint happening) so people seem to be happy with the results they get when using my site's search.
If you think I missed anything, or tried it yourself and it worked well, I'd be interested in hearing about it.
Update — 11-08-2012
The frame got very annoying; results clicked of course opened up with the frame, and then browsing within the frame got a little dicey when having to scroll both vertically and horizontally. I have to nix the DDG method and opted for the Relevanssi plugin. DuckDuckGo is still my personal search engine though, and I still recommend it for everyday use.