Welcome to the Question2Answer Q&A. There's also a demo if you just want to try it out.
+13 votes
in Q2A Core by
edited by

I know that there is a google custom search plugin by sama55, but with only 1 core hack you are able to setup Google Custom Search. It took me a while to figure out the steps, that's why I help you save some time! This is how I did:

1. go to q2a: >Admin >Pages and click "Add Page" Button, name your page e.g. "gsearch" (for the URL)

2. Edit Page and insert code from google search engine which should look like this:

  (function() {
    var cx = 'partner-pub-6400847838975858:9506492080';
    var gcse = document.createElement('script');
    gcse.type = 'text/javascript';
    gcse.async = true;
    gcse.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') +
        '//www.google.com/cse/cse.js?cx=' + cx;
    var s = document.getElementsByTagName('script')[0];
    s.parentNode.insertBefore(gcse, s);

<!-- add this extra line (!) -->

3. Core hack: Open /qa-include/qa-page.php
find line:
'form_tags' => 'METHOD="GET" ACTION="'.qa_path_html('search').'"',
replace it with:
'form_tags' => 'METHOD="GET" ACTION="'.qa_path_html('gsearch').'"',

4. upload file to server


additional steps:

- Create your search engine, i.e. go to http://www.google.com/cse/ click on New search engine, enter your domain

- Go to "Look and feel", choose "Two column" layout, click "Save and Get Code"

- Copy javascript code displayed

Can't you just override the appropriate function in the theme that outputs the search box?
+1, yes, that would be the proper way :)
Could this be used with adsense?
Does this hack even now? I tried but results not coming.
Thanks - got it to work, issue was the query parameter being changed to gsearch and I had added the gcse script elsewhere on the page also.
edited by
Thank you! This is still working for 1.8 beta 1 but I edited qa-include/app/page.php and not file you mentioned.
edited by
Great solution, because no data is transferred to Google unless a user actually uses thea search (privacy/cookie issue).

In qu-include/app/page.php I also had to add the "g" here:

In context:

$qa_content['search'] = array(
        'form_tags' => 'method="get" action="' . qa_path_html('gsearch') . '"',
        'form_extra' => qa_path_form_html('gsearch'),

Also make sure not to forget the last (gray) step in the manual above: Go to  https://cse.google.com/cse/ and obtain a key (the "cx" parameter) for your URL.

2 Answers

+2 votes

In Q2A V 1.8 :

3. Core hack: Open  /qa-include/app/page.php

line 641: 'form_tags' => 'METHOD="GET" ACTION="'.qa_path_html('search').'"', 
replace it with: 
'form_tags' => 'METHOD="GET" ACTION="'.qa_path_html('gsearch').'"',

what is the advantage of this? When I implement this it slow down my site speed
can read this article:
Benefits of Using Google Custom Search Engine (CSE) (https://goo.gl/nbYV3V)
+1 vote

Instead of core hacks which may not be valid after version updates. Try overriding search() function.

    public function search()
        $search = $this->content['search'];

        $this->output('<div class="qa-search"><form METHOD="GET" ACTION="'.qa_opt('site_url').'gsearch">',