InfoHeap
Tech
Navigation
  • Home
  • Tutorials
    • CSS tutorials & examples
    • CSS properties
    • Javascript cookbook
    • Linux/Unix Command Line
    • Mac
    • PHP
      • PHP functions online
      • PHP regex
    • WordPress
  • Online Tools
    • Text utilities
    • Online Lint Tools
search

Wordpress customization tutorials

  • Handle pagination in wordpress custom php code
  • How to and why disable wordpress cron
  • How to customize wordpress image alt tag
  • How to display date in wordpress pages
  • How to display wordpress page list with specific custom field value
  • How to display wordpress top level pages
  • How to hide a post from home and RSS feed in wordpress
  • How to include bootstrap javascript and css in wordpress post
  • How to include wordpress pages in archives
  • How to override priority and change frequency in Yoast xml sitemap
  • How to remove xmlrpc from wordpress headers
  • How to setup wordpress custom query params with pretty url format
  • How to show wordpress pages on front page with skip_home custom field
  • How to use google custom search for wordpress site
  • How to write custom php in wordpress
  • No frills social share links for Wordpress sites
  • Using Wordpress custom field for displaying code
  • Wordpress - add content filter after shortcode execution
  • Wordpress - customize category and tag links
  • Wordpress - customize facebook plugin opengraph meta tags
  • Wordpress - customize posts per page for tag, category and date archive pages
  • Wordpress - exclude specific posts from archive pages
  • Wordpress - get posts/pages with missing meta key
  • Wordpress - how to add filter to description meta tag
  • Wordpress - how to create custom tag cloud
  • Wordpress - how to exclude specific tag posts from a tag archive page
  • Wordpress - write custom php log to separate file
  • Wordpress how to check if a post is being viewed by admin
 
  • Home
  • > Tutorials
  • > Wordpress
  • > Wordpress customization

How to use google custom search for wordpress site

By admin | Last updated on Mar 20, 2016

WordPress comes with a default search which searches posts and pages from your site. It works pretty decent. Here is why you should consider switching to Google custom search:

  1. In case you serve multiple urls from same page, wordpress will still treat it as one page. So it won’t search them separately. This may be the case if you are serving multiple urls using query parameters from same page.
  2. WordPress search is based on mysql which may become slow.
  3. In case you have multiple wordpress in multiple subdomains or sub directories, Google custom search can search all of them.

Steps needed on Google Adsense site and wordpress admin UI

  1. First go to Google Adsense account page and navigate to My Ads -> custom search engines. In case custom search engine is not visible, you may have to go to other products and enable adsense for search. Then click on “new custom search engine”.
    google-custom-search-engine-create
  2. Fill the custom search engine name (any name you can relate to this engine later is fine), and choose “only sites I select” and enter http://yoursite.com/. Replace yoursite.com with whatever is your site url.
    google-custom-search-engine-create-main-options
  3. Take default options for Custom Channels, Search box style, and Ad style. You can change them later if you want to.
  4. Click on search results options and choose “on my web site using iframe”. In “url where search results will be displayed” enter http://yoursite.com/search/. You will also need to create a page in wordpress with permalink http://yoursite.com/search/.
    google-custom-search-create-search-box-stylePlease note that creating a page in wordpress is needed for search results to get displayed. You can choose some other name instead of http://yoursite.com/search/. Click on save and get code.
  5. You will see search box and search result code now.
    google-custom-search-box-result-codeCopy search result code and paste it to http://yoursite.com/search/ page you created. Since it is html code, you will have to switch to Text mode on wordpress admin edit screen. Here is how it should look:

    <div id="cse-search-results"></div>
    <script type="text/javascript">// <![CDATA[
    var googleSearchIframeName = "cse-search-results";
    var googleSearchFormName = "cse-search-box";
    var googleSearchFrameWidth = 800;
    var googleSearchDomain = "www.google.com";
    var googleSearchPath = "/cse";
    // ]]></script>
    <script type="text/javascript" src="http://www.google.com/afsonline/show_afs_search.js"></script>
  6. Keep a note of search box code. We’ll need it later in wordpress custom plugin.

Steps needed in wordpress custom plugin

  1. Write a simple plugin for your site containing functions.php file.
  2. Add filter for get_search_form
    add_filter( 'get_search_form', 'google_custom_search_form_filter' );
  3. Implement the function to replace search form with the google custom search code. It should look like this:
    function google_custom_search_form_filter () {
    $src =<<<EOM
    <form action="http://yousite.com/search/" id="cse-search-box">
    <div>
    <input type="hidden" name="cx" value="partner-pub-xxxxxxxxxxxxxxxx:xxxxxxxxxx" />
    <input type="hidden" name="cof" value="FORID:10" />
    <input type="hidden" name="ie" value="UTF-8" />
    <input type="text" name="q" size="20" />
    </div>
    </form>
    <script type="text/javascript" src="http://www.google.com/coop/cse/brand?form=cse-search-box&amp;lang=en"></script>
    EOM;
    return $src;
    }
  4. Once your custom plugin is active, you should see the Google custom search box on your site.
    google-custom-search-final-look
  5. Now type a query and search. The site url in browser should change to something like this:
    http://yoursite.com/search/?cx=partner-pub-......

    Now you should be able to see results using Google custom search.

Suggested posts:

  1. How to use w3 total cache for wordpress
  2. Ssh automation on Amazon EC2 Ubuntu Linux
  3. How to setup wordpress custom query params with pretty url format
  4. How to hide a post from home and RSS feed in wordpress
  5. Mac brew (homebrew) – quick start guide
  6. Traceroute outcome from Bangalore to AWS Virginia and California
  7. How to display date in wordpress pages
  8. Vim – how to go back to last edited line/context
Share this article: share on facebook share on linkedin tweet this submit to reddit
Posted in Tutorials | Tagged Tutorials, Web Development, Wordpress, Wordpress customization
  • Browse content
  • Article Topics
  • Article archives
  • Contact Us
Popular Topics: Android Development | AngularJS | Apache | AWS and EC2 | Bash shell scripting | Chrome developer tools | Company results | CSS | CSS cookbook | CSS properties | CSS Pseudo Classes | CSS selectors | CSS3 | CSS3 flexbox | Devops | Git | HTML | HTML5 | Java | Javascript | Javascript cookbook | Javascript DOM | jQuery | Kubernetes | Linux | Linux/Unix Command Line | Mac | Mac Command Line | Mysql | Networking | Node.js | Online Tools | PHP | PHP cookbook | PHP Regex | Python | Python array | Python cookbook | SEO | Site Performance | SSH | Ubuntu Linux | Web Development | Webmaster | Wordpress | Wordpress customization | Wordpress How To | Wordpress Mysql Queries | InfoHeap Money

Copyright © 2025 InfoHeap.

Powered by WordPress