WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] Google Custom Search in WordPress

[Resolved] Google Custom Search in WordPress

  • I’m trying to apply the Google Custom Search Engine in my WordPress installation, and the results page show up blank.

    I followed two different kind of instructions but both failed on me.

    The first one, explained here, describes how you set up a page (Write > Page) and paste the JavaScript code into the text area (obviously in HTML).

    The second one, explained here, requires you to set up a template page (_search.php) in your theme directory and then create the page using that template.

    Both of those tips were applied at different times. Both of those failed me. My blog is here and you can tell that if you try to perform a search, a blank page shows up.

    That apparently was a big issue for others as well. But it worked for many people, so I’m inclined to think there’s some small thing I’m missing here.

    The question is – what?

Viewing 15 replies - 1 through 15 (of 20 total)
  • I use a Google Custom Search on my blog, but I set it up so long ago that I forgot how I did it. 🙂

    Looking closer at it, it appears that I created a Page called “search-results” with the necessary javascript code in it. Then I pasted the search box code into a sidebar widget.

    Looking at your code, it would seem to me that your var googleSearchIframeName is set wrong. Try changing it to this:
    var googleSearchIframeName = "results_014170601555854922742:d3x1hsdptlk";

    Might work.

    More to the point though, both bits of code on your page are not the verbatim code from the http://www.google.com/coop/cse/ site. You can’t change those, they need to be exactly what Google’s site gives you.

    Wish I could say that that did it, but it doesn’t seem to work 🙁

    I don’t think the issue is in the Google code that they provided to me… I think something in the theme causes blank pages for some and not for others. I really have NO idea though, and it’s driving me batty.

    nathanrice
    Member

    @nathanrice

    try asking this in the theme support forum.

    I’ll give it a try, thanks nathan. Hope the double posting won’t tick anyone off. I really don’t know for sure if it’s a theme problem, but my guess is that if it impacts some people and not others, that would be it.

    I think something in the theme causes blank pages for some and not for others.

    No, that’s not it. I get the blank page too, but I see the javascript code there just fine. This is definitely an issue with the javascript that you are using.

    Weird. Unfortunately for me, Google is not correctly filling out the variables in the search box. The code they’re providing is generic.

    <!-- Google Search Result Snippet Begins -->
    <div id="cse-search-results"></div>
    <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>
    
    <!-- Google Search Result Snippet Ends -->

    I applied your changes (see my search.php file now), but this is way frustrating 🙁

    btw, I tried this, but still, a blank page:

    <!-- Google Search Result Snippet Begins -->
    <div id="results_014170601555854922742:d3x1hsdptlk"></div>
    <script type="text/javascript">
      var googleSearchIframeName = "results_014170601555854922742:d3x1hsdptlk";
      var googleSearchFormName = "searchbox_014170601555854922742:d3x1hsdptlk";
      var googleSearchFrameWidth = 400;
      var googleSearchDomain = "www.google.com";
      var googleSearchPath = "/cse";
    </script>
    <script type="text/javascript" src="http://www.google.com/afsonline/show_afs_search.js"></script>
    <!-- Google Search Result Snippet Ends -->

    Okay, figured it out after spending about 7 hours straight on this PITA.

    Google wasn’t generating the code properly. I copied and pasted from another site and modified the variables and it seems to be fine now.

    Yeah, I kind of wish I had a better solution to offer, but this is it. 🙂

    lazyazian
    Member

    @lazyazian

    Can you post where you got the code? and what vars you modifiied?

    I’m having the same exact issue.

    thanks!

    Sorry lazyazian – didn’t mean to leave you there hanging.

    Here’s the information that I used.

    For the search box:

    <!-- Google CSE Search Box Begins -->
      <form id="searchbox_014170601555854922742:d3x1hsdptlk" action="http://www.techipedia.com/search/">
        <input type="hidden" name="cx" value="014170601555854922742:d3x1hsdptlk" />
        <input type="hidden" name="cof" value="FORID:11" />
        <input name="q" type="text" size="20" />
        <input type="submit" name="sa" value="Search" />
      </form>
    
      <script type="text/javascript" src="http://google.com/coop/cse/brand?form=searchbox_014170601555854922742:d3x1hsdptlk"></script>
    <!-- Google CSE Search Box Ends -->

    On the search page:

    <div id="content_cse">
    <!-- Google Search Result Snippet Begins -->
    <div id="results_014170601555854922742:d3x1hsdptlk"></div>
    <script type="text/javascript">
      var googleSearchIframeName = "results_014170601555854922742:d3x1hsdptlk";
        var googleSearchFormName = "searchbox_014170601555854922742:d3x1hsdptlk";
          var googleSearchFrameWidth = 400;
            var googleSearchDomain = "www.google.com";
          var googleSearchPath = "/cse";
          </script>
          <script type="text/javascript" src="http://www.google.com/afsonline/show_afs_search.js"></script>
          <!-- Google Search Result Snippet Ends -->
    
    </div>

    You WILL need to fill in the variables yourself — 014170601555854922742:d3x1hsdptlk is specific to my CSE and won’t work. Similarly, my code shows my domain — edit it for your search page. 🙂

    Good post, thanks!

    tamar,
    I tried doing what you said and it didn’t work. Let me be sure I did what you did to get it work…

    First, you never said which of the 2 methods you did but I assumed you created the _search.php one. Second, you pasted both search box and search results code into the code that that web page’s directions, but motified the code so it looked like your most recent post on this forum. You uploaded the file to your wordpress directory.

    Third, you made a blank page and selected the theme for cse. You didn’t paste anything into this page?

    I just get a page with the search box that then redirects me to my index.php page. I get no results. =( Let me post my code:

    <?php
    /*
    Template Name: SearchResultsFromCSE
    */
    ?>
    <?php get_header(); ?>
    <div id="content_cse">
    <!-- Google CSE Search Box Begins -->
    <form action="http://beta.slythefox.com/?page_id=104" id="searchbox_3080183218079083:wu4ugre2kz3">
      <div>
      <input type="hidden" name="cx" value="3080183218079083:wu4ugre2kz3" />
        <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" />
      </div>
    </form>
    <script type="text/javascript" src="http://www.google.com/coop/cse/brand?form=searchbox_3080183218079083:wu4ugre2kz3"></script>
    <!-- Google CSE Search Box Ends -->
    <!-- Google Search Result Snippet Begins -->
    <div id="results_3080183218079083:wu4ugre2kz3"></div>
    <script type="text/javascript">
      var googleSearchIframeName = "results_3080183218079083:wu4ugre2kz3";
      var googleSearchFormName = "search-box_3080183218079083:wu4ugre2kz3";
      var googleSearchFrameWidth = 400;
      var googleSearchDomain = "www.google.com";
      var googleSearchPath = "/cse";
    </script>
    <script type="text/javascript" src="http://www.google.com/afsonline/show_afs_search.js"></script>
    <!-- Google Search Result Snippet Ends -->
    </div>
    <?php get_sidebar(); ?>
    <?php get_footer(); ?>

    Never mind, I got it working. I created a search.php and pasted the code into it but uploaded the file into my main wp directory, not the theme directory, as some sites suggested. It’s all great and no modifying of google code was needed.

    TheSooFoo
    Member

    @thesoofoo

    I have tried all of this but am still unable to the Google Custom Search to show any results.

    Saijo George
    Participant

    @1800pocketpc

    WordPress users can actually go I more step and integrate the Adsense Search to the WordPress page itself , so the search looks much more like its a part of the site rather than being redirected to a page by Google.

    Look @ the search on my blog or this image to see what i mean : http://img80.imageshack.us/img80/9720/searchintegrationso7.png

    Tutorial : http://www.saijogeorge.com/2008/10/03/google-adsense-search-integration-in-to-wordpress.html

Viewing 15 replies - 1 through 15 (of 20 total)
  • The topic ‘[Resolved] Google Custom Search in WordPress’ is closed to new replies.