Swap WordPress 2015 search for Google Custom Search

Home Forums Web Hosting Forum Swap WordPress 2015 search for Google Custom Search

This topic contains 2 replies, has 2 voices, and was last updated by  Anna 4 years, 1 month ago.

  • Author
    Posts
  • #2574

    Anna
    Participant
    Ŧollars: Ŧ 41.45



    I’m a big fan of the WordPress 2015 theme, as a starting point for a site under organic development.

    However, I prefer to use Google Custom Search as it gives better results, which can be monetized.

    Where should I change my Child theme? Or, is there a plugin or snippet to make it easier if/when I change theme?

  • #2595

    Keith Taylor
    Keymaster
    Ŧollars: Ŧ 1 ,191.16

    Yes, Google Custom Search is better than the basic WordPress search in almost all cases (I’m happy to discuss the times it isn’t the best choice, if anyone is interested).

    I covered the basics in Find Fast WordPress Hosting. As you see, you need a page to display results that does not show AdSense adverts. Please let me know if you need help with that, or with my explanation of how to get the Custom Search code.

    To get the search box, you have two choices. You can change the theme searchform, as I explain in that article, or you can create your own Custom Search widget, as I currently do on this website.

    To create your own Custom Search Widget, add a Text widget to your sidebar, and give it a title. Paste Google’s Search Box code into the widget content box:

    <script>
      (function() {
        var cx = '017720281656747853863:f4yqwvqz8gu';
        var gcse = document.createElement('script');
        gcse.type = 'text/javascript';
        gcse.async = true;
        gcse.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') +
            '//cse.google.com/cse.js?cx=' + cx;
        var s = document.getElementsByTagName('script')[0];
        s.parentNode.insertBefore(gcse, s);
      })();
    </script>
    <gcse:searchbox-only resultsUrl="http://shrewdies.net/search/"></gcse:searchbox-only>
    

    Note that you must replace cx and resultsUrl with your own values.

    If you are not sure if this Widget Search or the alternative searchform.php method is best, let’s discuss it. I’d also like to know what other people think about using a function to try and achieve a better search box. This would remove the need to change searchform or widget if you change theme. However, it seems like overkill to code a method that would suit every theme?

    What do you think?

  • #2612

    Anna
    Participant
    Ŧollars: Ŧ 41.45

    Thanks, that works great.

    I see the Custom Search Engine script is loaded with the search box in the Widget, and on the search results page. Is there likely to be a time penalty for loading the script twice? Is it possible to load it after the page has loaded? I ask this because I think it would be quite neat for the page to load completely, then the search box to appear afterwards.

You must be logged in to reply to this topic.