How To Replace Your Wordpress Search With Google
Google is the undisputed king of search. They do it well. What many bloggers may not be aware of is that you can use Google as your site’s main search engine. You don’t have to use your blog engine’s built in search.
There are two really nice benefits to using Google for your site search engine:
- It’s Google! That means you have full power of Google search on your blog.
- You can use Adsense for your search results and make some money on your site searches. And the CPM is usually VERY high with Adsense in this case because the ads are super well targeted.
So the question is: how do you do it? Well, I’ll show you.
- You need an Adsense account. So, if you’re not already signed up as an Adsense publisher, do so now.
- In your Adsense account, click on “Adsense Setup”. Then click on “Adsense for Search”.
- Click the “Get Started” button. You will enter the wizard for creating your search form.
- For “Search Type”, select “Only sites I select”. In your “Selected Sites” box, type the URL to your blog. You can enter multiple sites if you wish.
- Enter some “optional keywords” which will gear the ads displayed toward your blog subject matter.
- You can leave all other fields as default. Continue to the next step.
- Choose the look and feel of your search form. Continue to the next step.
- Next, you can choose where you want your search results to appear. In order to make the search results appear within your own site, choose “open results within my own site” and specify the URL. For PCMech, I entered “http://www.pcmech.com/searchresults.php”. This is obviously not part of the Wordpress installation, but that’s OK. We’re going to create this file for you in a moment.
- Choose the ad position and color palette and then continue to the next step
- Assign a name to the search engine. This is for your own internal purposes later. Then hit the “Save and Get Code” button.
- You’re going to get two snippets of code. One is for the actual search form and the other is for your search results page. Include the search form HTML wherever you want it to appear in your blog theme (likely either the sidebar or the header). You can use CSS to customize the appearance of the search form to match your site as closely as possible.
- Now you need to create a file called searchresults.php. The code I use for this file is below. You will need to paste in your results code that Google gave you, modify as needed to suite your particular theme, and upload to your blog’s root directory.
<?php
unset($_SERVER['PATH_INFO']);
//Include current WordPress Theme Header etc.
require(’./wp-config.php’);
function maketitle() {
return ” | Search Results”;
}
//Check if we’re wrapping the WP Theme
//Get Theme settings.
$themes = get_themes();
$current_theme = get_current_theme();
$current_template_dir = $themes[$current_theme]['Template Dir'];
$current_stylesheet_dir = $themes[$current_theme]['Stylesheet Dir'];
//Initialize the WP class to be able to get the header
wp();
//Set status to 200 to override the 404 set by WordPress
status_header(200);
//Include the WP Header
add_filter(”wp_title”,’maketitle’);
get_header();
?>
[ PASTE YOUR SEARCH RESULTS CODE FROM GOOGLE HERE ]
<?php get_sidebar(); ?>
<?php
get_footer();
?>
This file will use your blog’s active theme, however you may need to do some modifications to suit your blog. You might also need to go back and define the width of the search results to fit your blog.
When you’re done, you will have a Google powered search engine within your blog.
NOTE: The only caveat to keep in mind here is that your posts will not appear in your search engine until Google indexes you. However, if you post often and use the XML Sitemaps plug-in, you should be fine.
If you enjoyed this article, you might also like...
- How To Use a Custom Page Template In Wordpress
- Solution - Wordpress Theme Reverts to Default (Kubrick)
- Lazy Linkage - Wordpress, Universal Search
- Setting up a Gallery with Wordpress
- Writing Headlines For Social Media or Search Engines?
Comments
Really good step through.
It doesn’t look like you’re using it on this blog yet. Is it something you plan to do?
This is great! Thanks for providing tips on things I can put into action to grow my blogs instead of just posts that make me say oh that’s nice.
[...] How To Replace Your Wordpress Search With Google | David Risley dot com (tags: wordpress search google hacks risley) [...]
When I checked the operation of the new Google search I got a parse error:
‘Syntax error: Unexpected “/” in line 6′.
Being a php novice I couldn’t work out exactly what had gone wrong, so I temporarily removed the Google search bars from the blog pending further investigation. I appear to have copied everything exactly, and followed instructions to the letter…?
The great part about this is that it is the whole procedure, for the most part. Someone that wants to make the switch to using Google search on their own site will remember to bookmark this or search for it when they are planning to do it. It makes sense that Google would require AdSense setup before their services could be used.
Has anyone else figured out how to correct this error. It’s similar to the one Sharron had above
Parse error: syntax error, unexpected ‘/’ in … on line 4
I keep changing things but can’t get it to work.
Thanks.
In reply to Malaika Morris:
This is rather weird; as your script is seemingly showing a syntax error on line 4, from what I read above, and mine is showing on line 6. As far as I can see there are no instances of “/” on lines 4 or 6; unless I’m reading it wrong. I take it we’re both using the same script?
I’m still a php ameteur at best; so I’ve shelved this project for now pending review on David’s return. The search results page script is still on my server, but I’ve removed the search box scripts from the pages and sidebar where I’d inserted them for the time being as they’re not working and returning the parse error.
An even better strategy is to use Google to search across a network of your sites if you have more than one… something I am working on at the moment.
Jon
http://2xStocks.com
Doubling my money, one stock at a time
Jon, please let us know more about your plan to use Google to search across a network of sits. I’d love to do something like that for several of my sites.
To those with parsing errors: I’m receiving the same error and have had to remove the Google search boxes until I get it figured out or a post here gives the solution. Anyone with any ideas? I’m willing to try various things and promise not to flame you if it doesn’t work (:
I like the idea, but it seems quite hard to change the style of google search results page, while my theme has very nice layout of search results.
Thank you so much for this alternative for integrating Google Search into Wordpress.
So far it’s working great on my blog.
Hi David,
Thank you for the tip. I agree with you, that many bloggers certainly are not aware of the fact, that they can use Google as their search engine.
For those with the parsing errors, if you just did a copy and paste on the code you need to manually edit the searchresults.php file with a text editor and change the ’s in line’s 4 and 6. If you need more help post your error.





Thanks David, I did this for all of my sites, gettingpumped.com - userbytes.com - and of course seizethepage.com
Thanks.