• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

Trying to add a Google CSE custom search to the forum

Stuart Wright

Well-known member
Hi folks,
I'm using the two page solution for Google CSE.
A query parameter, q, is passed to the results page.

I found a code snippet for a callback on a page which Jake posted for user id:

class Callback_PageNode
public static function getUser(XenForo_ControllerPublic_Abstract $controller, XenForo_ControllerResponse_Abstract &$response)
$userId = $controller->getInput()->filterSingle('u', XenForo_Input::UINT);

$userModel = XenForo_Model::create('XenForo_Model_User');
$user = $userModel->getFullUserById($userId);
$user = $userModel->prepareUser($user);

$response->params['user'] = $user;
Obviously q is going to be a string, though.
I'm assuming I have to change XenForo_Input::UINT to a string.
and then just return $response->params['q'] = $q;
Which XF function is it I would have to use instead of
$q = $controller->getInput()->filterSingle('q', XenForo_Input::UINT);

Stuart Wright

Well-known member
Ok so I dug around and I should have guessed it's as simple as XenForo_Input::STRING.
So I created the controller php script and uploaded it and saved the page with the controller stuff in there ok.
But the template for the page needs to have the variable $q available for the Google CSE search results.
The search results page template looks like this.
<div class="baseHtml ugc">
<xen:set var="$q">{xen:raw $q}</xen:set>
  (function() {
    var cx = 'partner-pub-2615987788629632:lme0jj5mawk';
    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);
No search results are displayed, I'm guessing because the value of q is not available to the script.