Advertisement:

Author Topic: Adding to the search URL  (Read 1833 times)

Offline Study Force

  • SMF Hero
  • ******
  • Posts: 3,656
    • StudyForcePS on Facebook
    • @studyforceps on Twitter
Adding to the search URL
« on: October 20, 2018, 01:53:30 PM »
I'm trying to enhance the search functionality on the forum, so I'm looking to add a Google Custom Search whenever nothing is found in the database for a search.

What I did was add:

Code: [Select]
$type = 'MY_SPECIFIC_ID';

$context['html_headers'] .= '
<script>
(function() {
var cx = \''.$type.'\';
var gcse = document.createElement(\'script\');
gcse.type = \'text/javascript\';
gcse.async = true;
gcse.src = \'https://cse.google.com/cse.js?cx=\' + cx;
var s = document.getElementsByTagName(\'script\')[0];
s.parentNode.insertBefore(gcse, s);
})();
</script>';

Inside the argument:

Code: [Select]
if (!empty($context['search_errors']))
Found in the function PlushSearch1 - this just calls the script.

Inside the search template, I added:

Code: [Select]
if(!empty($context['search_errors']) && !empty($context['search_params']['search']))
echo'
<gcse:searchresults-only></gcse:searchresults-only>';

That part works fine. HOWEVER, for Google Custom Search to work, I need to be able to add the following to the URL:

/index.php?action=search2&cx=MY_SPECIFIC_ID&cof=FORID%3A10&ie=UTF-8&q=SEARCH_QUERY&sa=Search

As a result, I went inside Search.php and found:

Code: [Select]
if (empty($indexedResults) && empty($numSubjectResults) && !empty($modSettings['search_force_index']))
{
$context['search_errors']['query_not_specific_enough'] = true;
$_REQUEST['params'] = $context['params'];
return PlushSearch1();
}

How can I manipulate the code so that when nothing is found, it adds what's underlined above into the URL?

Been working on this for hours. :-\