Welcome to the Question2Answer Q&A. There's also a demo if you just want to try it out.
+14 votes
3.3k views
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:

<script>
  (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);
  })();
</script>

<gcse:searchbox></gcse:searchbox>
<!-- add this extra line (!) -->
<gcse:searchresults></gcse:searchresults>

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

done!

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

by
Does this hack even now? I tried but results not coming.
by
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.
by
edited by
+1
Thank you! This is still working for 1.8 beta 1 but I edited qa-include/app/page.php and not file you mentioned.
by
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:
qa_path_form_html('gsearch')

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
by

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').'"',

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

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">',
            @$search['form_extra']
        );

        $this->search_field($search);
        $this->search_button($search);

        $this->output(
            '</form>',
            '</div>'
        );
    }   

...