WordPress Search using DuckDuckGo

WordPress has a built-in search feature which allows a site using self-hosted WordPress to search local pages and posts, and then display the results on the site. What if you want to use an external search engine to do this instead?

Why use an external Search Engine?

The Built-in WordPress search feature can be slow, and can cause performance issues if you run a large WordPress website.

Also, if you want to create a static website from your WordPress site you need to change the search feature to use an external search engine.

Important: This is for self-hosted WordPress websites, not a wordpress.com website.

What Search Engine to use?

There are a few options. The obvious choice is Google Custom Search Engine, because Google knows search.

DuckDuckGo search engineHowever, a more privacy friendly alternative would be beneficial to your users. Since unfortunately not all users are using Firefox for privacy with a content blocking add-on. This is where DuckDuckGo comes to the rescue.

How to use WordPress Search using DuckDuckGo

There’s a few options for adding a search box to your WordPress self-hosted website.

DuckDuckGo has basic instructions for adding a search box. More specific information for WordPress is below.

Custom HTML widget

The easiest way is to add a “Custom HTML” widget to wherever you need a search box.

<form action="https://duckduckgo.com/">
<input type="hidden" id="sites" name="sites" value="ryandaniels.ca">
<input type="search" placeholder="Search &hellip;" value="" name="q" />
<button type="submit" class="search-submit"></button></form>

This is creating a form which is sent to DuckDuckGo search, using a hidden field with the value of your domain. Be sure to change it from “ryandaniels.ca”!

Of course, you will need to make some changes with CSS to make this fit into your theme.

Also be careful, other places could be using the built-in search, like on an error page. The next option solves this problem.

Create a Search Page in WordPress

If you are familiar with modifying WordPress themes this is the best and easiest method.

Create a custom search form (searchform.php file) using the WordPress Codex documentation, and be sure to put it into your WordPress child theme folder. The benefit is there’s no need to mess around with CSS, and if your theme uses a search box on a 404 page, this will automatically work!

As an example, this is based on the Twenty Seventeen WordPress theme. In your “child theme” folder, create a new file called searchform.php with the contents:

<?php
/**
* Template for displaying search forms in Twenty Seventeen
*
* @package WordPress
* @subpackage Twenty_Seventeen
* @since 1.0
* @version 1.0
*/

?>

<?php $unique_id = esc_attr( uniqid( 'search-form-' ) ); ?>

<!-- <form role="search" method="get" class="search-form" action="<?php echo esc_url( home_url( '/' ) ); ?>"> -->
<form role="search" method="get" class="search-form" action="https://duckduckgo.com/">
<label for="<?php echo $unique_id; ?>">
<span class="screen-reader-text"><?php echo _x( 'Search for:', 'label', 'twentyseventeen' ); ?></span>
</label>
<input type="hidden" id="sites" name="sites" value="<?php echo esc_url( home_url() ); ?>">
<input type="search" id="<?php echo $unique_id; ?>" class="search-field" placeholder="<?php echo esc_attr_x( 'Search &hellip;', 'placeholder', 'twentyseventeen' ); ?>" value="<?php echo get_search_query(); ?>" name="q" />
<button type="submit" class="search-submit"><?php echo twentyseventeen_get_svg( array( 'icon' => 'search' ) ); ?><span class="screen-reader-text"><?php echo _x( 'Search', 'submit button', 'twentyseventeen' ); ?></span></button>
</form>

The only new or modified lines are the “<form …” to tell the search box to use DuckDuckGo.
The hidden input name to use your site.
The search input name is now “q” instead of “s”.

Now anywhere on your site that uses a search box will use this new search form! You can add a search widget to your sidebar and it will go to DuckDuckGo.

Use a WordPress Plugin

WP Google Search
DuckDuckGo Plugin (unfortunately no longer available)

Other options

These are other options, which probably aren’t a good idea. I’ll list them anyways. But just don’t.

Use .htaccess to redirect the search query yourdomain.com/?s=search before it gets to WordPress. Since WordPress redirects searches to /?s=search (where search is the search text), you could use .htaccess redirects to catch the /?s= and do a redirect to an external search engine. Why this is a bad idea? Don’t have any root directory redirects, it could hurt your SEO (used for search engine ranking). There’s also a performance impact having Apache (or nginx) web servers perform this operation on your root web directory!

Create a PHP page to do the redirect. This can also work, but unless you need more control of what’s sent to the external search engine, the other options work fine with less overhead.

Conclusion

Now you have created a custom search form on your WordPress site. You’ve reduced your overhead needed by WordPress and now have the option to setup a static WordPress website!

Want to try a demo? Use the search box on my site! I created a custom search page in my WordPress theme.