WordPress Search as Custom Google Search Without Additional Page or Post

It’s been a while since I wrote an article. That’s because I have been concentrating on developing netsains.com during the month. Thanks for that, I learn many things about customizing WordPress and creating WordPress as CMS. It’s way fun than designing the website itself, I must admit. One of the things that I modify is the default WordPress Search to be seen as Custom Google Search without using any plugins, additional post, or page. It’s simply replacing WordPress Search with Custom Google Search.

The first thing to learn is to create Custom Google Search account of your own. Maki from Doshdosh.com already created the tutorial on embedding Custom Google Search in his website. However, I have my own tricks on my sleeves to this. Let’s start with requirements.

To create Custom Google Search in WordPress, you need:

  1. A self hosted WordPress blog (you can’t modify things in shared hosting wordpress.com blog)
  2. A Google account (you can setup an account here)
  3. Any HTML editor (wordpad or notepad is fine)

First step is to create Custom Google Search:

  1. Login to Custom Google Search with your Google account from this link
  2. create custom search engine
    Choose “Create a Custom Search Engine”
  3. Fill in
    Fill in your search engine basic information
  4. next page
    Type your blog domain
  5. test
    Test your search engine. If the results aren’t satisfying, you can always revert back to the fist step
  6. Get the code
    Choose as mentioned above. We need different page to host our results, that’s why we choose iframe.
  7. Code
    The first code is your SEARCH FORM code. The second code is your SEARCH RESULTS code. Now your custom search engine is ready


The second step is to install your code in WordPress SEARCH FORM and SEARCH RESULTS:

  1. Your WordPress SEARCH FORM and SEARCH RESULTS can be found in your current template. This steps only occurs in your current template. If you want to edit the other template, please do repeat the steps
  2. Your WordPress SEARCH FORM is: yourblog/wp-content/themes/yourtemplate/searchform.php
    Your WordPress SEARCH RESULTS is: yourblog/wp-content/themes/yourtemplate/search.php
  3. Please do backup those two files before altering. In case that you want to revert back to the old search
  4. Go to your SEARCH FORM, paste the code for Custom SEARCH FORM (First code), the result will be like this:

    Edited, thanks to Rio

    <form action="http://yourblog.com/index.php?" id="cse-search-box">
    <div>
    <input type="hidden" name="cx" value="007584696391574650722:l_61re9sdqy" />
    <input type="hidden" name="cof" value="FORID:10" />
    <input type="hidden" name="ie" value="UTF-8" />
    <input type="text" name="q" size="31" />
    <input type="submit" name="sa" value="Search" />
    <input type="hidden" name="s" value="Search" />
    <input type="hidden" name="submit" value="" />
    </div>
    </form>
    <script type="text/javascript" src="http://www.google.com/coop/cse/brand?form=cse-search-box&lang=en"></script>

    The two bold lines of code are the compulsory codes to trigger Search Results in wordpress. The first line will define the keyword variable ($s) and the second line is the submission button ($submit). Those two lines won’t affect your custom search engine, however, you can’t erase it because WordPress will not open the Search Result page without those two variables.

    This is the example URL that will trigger WordPress Search Result: http://yourblog.com/index.php?s=keyword&submit=

    That’s why I can add any Google Custom Search variables as long as those two are unharmed :)

    If you are having trouble with the form layout, please do consult w3schools.com to learn about HTML and CSS

  5. Now go to your WordPress SEARCH RESULTS and replace the code into the following pattern:


    <?php get_header(); ?>

    ---put your google search results here---

    <?php get_footer(); ?>

    The result will be like this:


    <?php get_header(); ?>
    <script type="text/javascript">
    var googleSearchIframeName = "cse-search-results";
    var googleSearchFormName = "cse-search-box";
    var googleSearchFrameWidth = 600;
    var googleSearchDomain = "www.google.com";
    var googleSearchPath = "/cse";
    </script>
    <script type="text/javascript" src="http://www.google.com/afsonline/show_afs_search.js"></script>
    <?php get_footer(); ?>

  6. Your custom search engine is ready to go!

Custom Google Search Engine is very useful to your website. There are two reasons why you should use it:

  • Comprehensive Google Search, you know how powerful Google search is, imagine it putted in your website. You might also want to install Google Sitemap generator plugins to widen your search results from Google
  • Add money on it, yes, Google Search can be integrated with your Adsense channel. Check Doshdosh.com to learn how

Updated

Credit: Thanks to Rio for pointing out this issue

How to make your Google Search Terms appear in the page’s title

This issue is already explained clearly in Thu Tu’s Blog. The search terms will appear in your search page’s title in any theme you used. In order to make one of your theme to show the search terms, you can follow these steps:

  • Open your chosen theme’s header.php file.
  • Modify your theme’s title inside the <title> tag like this:

    <title>

    <?php

    $cse_query = strip_tags(stripslashes($_GET['q']));
    if (is_home()) {
    echo bloginfo('name'); echo ": "; echo bloginfo('description');
    } elseif (is_404()) {
    echo 'Page Not Found (Error 404)';
    echo ' - ';
    echo bloginfo();
    } elseif (is_category()) {
    echo 'Category : '; wp_title('');
    echo ' - ';
    echo bloginfo();
    } elseif (is_search()) {
    echo 'Search for: "';
    echo $cse_query;
    echo '" - ';
    echo bloginfo();
    } elseif (is_day() || is_month() || is_year() ) {
    echo 'Archive :'; wp_title('');
    echo ' - ';
    echo bloginfo();
    } else {
    echo wp_title('');
    $subtitle = get_post_meta($post->ID, 'subtitle', $single = true);
    if($subtitle !== '') { echo ': ' . $subtitle; }
    }

    ?>

    </title>

  • The first bold line will call your Google Search Terms (defined by variable $q), while the second bold line will call the variable and show it only in a search page (defined by is_search() wordpress function)
  • Remember that any of your installed WordPress plugin that can alter the Page Title (e.g. All in One SEO Pack) will make this method useless

The implementation of this method can be found in my latest site development: NetSains.com

Please have your comment and suggestion!

Comments

 30 comments

  • Thanks a ton. I always failed to integrate Custom search engine since google always reverted to full 795 width automatically in my 3 column theme. Your method worked just fine!

  • firewalker says:

    @Navjot Singh:

    Thank you for your feedback.

    The width for Google Search is defined from this variable:
    var googleSearchFrameWidth = 600;

    This way my search results is 600 pixels width.

  • hi,

    i tried this on my blog but when i replace the search.php, it messed my blog. i guess this trick is really theme dependent.

    what i did but is not really pleasant to see is to make another page instead and uploaded in the root directory and it worked. But the search box is looking like shezneck.

  • firewalker says:

    @Get it From Boy!

    Yes, it is theme dependent. In my theme, the default search.php only calls for Header and Footer.

    As for the search box, I leave it to your own creativity ;) .

  • rio says:

    hello, I’d like to know if it’s possible to make the show also the query terms, something like Search for “custom google” | Firewalker?

  • firewalker says:

    @rio:

    Yes, you can. As you can see at this code:
    <input type="hidden" type="text" name="s" value="Search" />

    You can change the value “Search” with any custom word that you want (this won’t affect the Google Search, only affect the Title page of your search result)

  • rio says:

    sorry firewalker,

    what I’d like to add is the query term which a user wanted to search.

    Like here in your search box, if i query [astalavista], the title of the search result shall be [Search for astalavista | firewalker]. I think it’s quite necessary if one need to track back his search history in the browser.

    thanks again,

  • rio says:

    Besides, the following two lines can’t pass w3c validation.

  • rio says:

    Those are the two lines u added in google search box, ate by WP, sorry

  • firewalker says:

    @Rio:

    Those two hidden lines have double Type attributes, it should be only one, which is “hidden”. Thanks for your sharing ;)

  • firewalker says:

    @Rio:

    Thanks for pointing out this issue and showing me the solution.
    And there we go, my custom title page solution ;)

  • First – I love your template! You do beautiful work!

    My theme doesn’t have a search or results page. I’ve created other “missing” pages in this theme in the past, based on the page template, so I’ll try doing the same this time. Do you know if I’ll need to modify any other files to tell the template it has those, now?

    Thanks!

  • firewalker says:

    @Cynthia:

    Thanks for the compliment.

    According to codex.wordpress in their article about search page, the file name search.php and searchform.php is already reserved in wordpress functions. So if you create a new one, it will recognize it as the default.

  • Love the theme, I am going to give it a try on a site of mine.

    This is a nice bit of code, it helps that you give a setp-by-step to explain (I need to explain this to someone that will not understand).

  • My above mentioned site is html built site and i am not able to show keyword based result of my website using google custom search i.e.when i am typing any keyword of my website in google custom search it is not showing any results from my website,but showing results from web.
    Please guide me for this problem.
    I am not able to show the pages in my website according to the keyword given in the search engine.

  • firewalker says:

    @Vijay Kumar

    Your site isn’t WordPress based so you should follow the usual Custom Google Search tutorial from the web.

    And the reason why your site is not shown in search result is because you are not pointing out your sites in the search engine. (see how to make custom google search)

  • Victor says:

    I have been trying to integrate the google search as instructed in this tutorial. However I am not having much success. I think it is because I do not have a searchform.php in my theme. The blog search form is integrated in the sidebar.php. So I have just pasted the first code that is the search form on the sidebar.php and gotten rid of the default search. When I do a search I does not display anything from google. Please help!!!

  • firewalker says:

    @Victor:

    If your theme didn’t have searchform.php, you can create your own and paste the search code just like I mentioned in the tutorial.

    Have you pasted the search code result generated by Google Custom Search Engine?

  • Meh. Looks like I’m stuck with WordPress’ search tool. I reeeaaaaally should move my blog to a self-hosted domain especially since I’m getting a lot of visitors daily.

  • sunshine says:

    Awesome job !!! keep it up.

  • Good share,you article very great, very usefull for us…thank you

  • stereobk.com says:

    Nice info. but i can’t get all information from this article. i have some post that i can’t translate. have your same article in indonesian?

    • firewalker says:

      Unfortunately no, I only make it in English for international visitors to use. But you can tell me which part that you didn’t understand and I’ll answer it in Indonesian :)

  • stereobk.com says:

    Oh its OK.
    I was repeat to read, and i was translate one by one word and now i was understand. hehe..
    sory if my english is broken.
    thanks for your tutorial.
    will be a great honor to me if you come to my simple homepage

 Give comment







logovcard

I am a website designer who create website that really works and easy to use. I am capable of creating website with XHTML, CSS, and jQuery. WordPress is my preferred tools of trade.

Saya adalah seorang desainer website yang mampu membuat website yang benar-benar berfungsi dan mudah digunakan. Saya dapat membuat website dengan XHTML, CSS dan jQuery. WordPress adalah piranti pilihan saya.

 
I am not available for design work at this time
Saya tidak bisa menerima pekerjaan desain untuk saat ini