Applies to:

What?
Posting this as it was rather rewarding to achieve and I hope will be of some use to others in the same boat. This article details how to replace jComments built-in kCaptcha with Google's ReCaptcha. Note that I have an article on how to do this with a ReCaptcha alternative called PlayThru by AreYouAHuman.com

Why?
The kCaptcha used by the jComments extension is easily automated and no longer blocks spam comments.

How?
So the main bulk came from the JoomlaTune website but as it was a forum post that may get moved/deleted, I've copied most of it and put it here with my minor revisions/adjustments to get it actually working.



STEP #1: Check ReCaptcha API
Check your recaptcha plugin is up-to-date as some Joomla 2.5.x systems will not have updated this:
  1. Open the file \plugins\captcha\recaptcha\recaptcha.php
  2. See if you have the line const RECAPTCHA_API_SERVER = "http://api.recaptcha.net";
  3. If you do, change it to const RECAPTCHA_API_SERVER = "http://www.google.com/recaptcha/api";

STEP #2: Check ReCaptcha Private Key
This is assuming you have already signed up for a ReCaptcha key (Google ReCaptcha)
  1. Login to your Joomla Administration panel
  2. Extensions > Plug-in Manager > Captcha - ReCaptcha
  3. Check "Public Key" contains your key.

STEP #3: Enable admin option dropdown
This is a code change to allow the admin to select recaptcha.
  1. Open the file \administrator\components\com_jcomments\admin.jcomments.php (line 1066)
  2. Replace the line
    $captcha[] = JCommentsHTML::makeOption('kcaptcha', 'KCAPTCHA');
  3. With this
    $captcha[] = JCommentsHTML::makeOption('kcaptcha', 'KCAPTCHA');
    $captcha[] = JCommentsHTML::makeOption('recaptcha', 'RECAPTCHA');

STEP #4: Set in admin setting
We've made the option available, now login and set it to our new captcha
  1. Login to your Joomla Administration panel
  2. Components > JComments > Settings > Layout > CAPTCHA > RECAPTCHA
  3. Save the change.

STEP #5: Display recaptcha in comment form
This step displays the ReCaptcha on the form (note this code may need to be reverted if the admin does not want to use recaptcha anymore)
  1. Open the file \components\com_jcomments\tpl\default\tpl_form.php (line 114)
  2. Replace the code
    <?php
    }
    if ($this->getVar('comments-form-captcha', 0) == 1) {
            $html = $this->getVar('comments-form-captcha-html');
            if ($html != '') {
                    echo $html;
            } else {
                    $link = JCommentsFactory::getLink('captcha');
    ?>
    <p>
        <span>
            <img class="captcha" onclick="jcomments.clear('captcha');" id="comments-form-captcha-image" src="/<?php echo $link; ?>" width="121" height="60" alt="<?php echo JText::_('FORM_CAPTCHA'); ?>" /><br />
            <span class="captcha" onclick="jcomments.clear('captcha');"><?php echo JText::_('FORM_CAPTCHA_REFRESH'); ?></span><br />
            <input class="captcha" id="comments-form-captcha" type="text" name="captcha_refid" value="" size="5" tabindex="6" /><br />
        </span>
    </p>
    <?php
            }
    }
    ?>
    
  3. With this
    <?php
    }
    if ($this->getVar('comments-form-captcha', 0) == 1) {
            $html = $this->getVar('comments-form-captcha-html','kcaptcha');
            if ($html == 'kcaptcha') {
                    $link = JCommentsFactory::getLink('captcha');
    ?>
    <p>
        <span>
            <img class="captcha" onclick="jcomments.clear('captcha');" id="comments-form-captcha-image" src="/<?php echo $link; ?>" width="121" height="60" alt="<?php echo JText::_('FORM_CAPTCHA'); ?>" /><br />
            <span class="captcha" onclick="jcomments.clear('captcha');"><?php echo JText::_('FORM_CAPTCHA_REFRESH'); ?></span><br />
            <input class="captcha" id="comments-form-captcha" type="text" name="captcha_refid" value="" size="5" tabindex="6" /><br />
        </span>
    </p>
    <?php
            } else {
                    $link = JCommentsFactory::getLink('captcha');
                    JPluginHelper::importPlugin('captcha');
                    $dispatcher = JDispatcher::getInstance();
                    $dispatcher->trigger('onInit','dynamic_recaptcha_1');
    ?>
    <p>
        <span>
            <div id="dynamic_recaptcha_1"></div>
        </span>
    </p>
    <?php
            }
    }
    ?>
    

STEP #6: Process submitted recaptcha
This step will add the functionality for your Joomla website to process the submitted captcha and verify it during form submission:
  1. Open the file \components\com_jcomments\jcomments.ajax.php (line 275)
  2. Replace the code
    if ($acl->check('enable_captcha') == 1) {                    
        $captchaEngine = $config->get('captcha_engine', 'kcaptcha');
        if ($captchaEngine == 'kcaptcha') {
            require_once( JCOMMENTS_BASE.DS.'jcomments.captcha.php' );
            if (!JCommentsCaptcha::check($values['captcha_refid'])) {
                self::showErrorMessage(JText::_('ERROR_CAPTCHA'), 'captcha');
                JCommentsCaptcha::destroy();
                $response->addScript("jcomments.clear('captcha');");
                return $response;
            }
        } else {
            $result = JCommentsEvent::trigger('onJCommentsCaptchaVerify', array($values['captcha_refid'], &$response));
            // if all plugins returns false
            if (!in_array(true, $result, true)) {
                self::showErrorMessage(JText::_('ERROR_CAPTCHA'));
                return $response;
            }
        }
    }
    
  3. With this
    if ($acl->check('enable_captcha') == 1) {
        $captchaEngine = $config->get('captcha_engine', 'recaptcha');
                                
        if ($captchaEngine == 'recaptcha') {                                                                                            
            $post = JRequest::get('post');  
            JPluginHelper::importPlugin('captcha');
            $dispatcher = JDispatcher::getInstance();
            $resp = $dispatcher->trigger('onCheckAnswer',$post['recaptcha_response_field']);                                                                                                            
            
            if (!$resp[0]) {
                self::showErrorMessage(JText::_('ERROR_CAPTCHA'), 'captcha');
                $response->addScript('Recaptcha.reload()');
                return $response;
            } else {
                $response->addScript('Recaptcha.reload()');
            }
        } elseif ($captchaEngine == 'kcaptcha') {
            require_once( JCOMMENTS_BASE.DS.'jcomments.captcha.php' );
            if (!JCommentsCaptcha::check($values['captcha_refid'])) {
                self::showErrorMessage(JText::_('ERROR_CAPTCHA'), 'captcha');
                JCommentsCaptcha::destroy();
                $response->addScript("jcomments.clear('captcha');");
                return $response;
            }
        } else {
            $result = JCommentsEvent::trigger('onJCommentsCaptchaVerify', array($values['captcha_refid'], &$response));
            // if all plugins returns false
            if (!in_array(true, $result, true)) {
                self::showErrorMessage(JText::_('ERROR_CAPTCHA'));
                return $response;
            }
        }
    }
    

Additional
  • To center the ReCaptcha on your form:
    1. Open the file \components\com_jcomments\tpl\default\tpl_form.php (~line 130)
    2. Replace the line <div id="dynamic_recaptcha_1"></div>
    3. With <div id="dynamic_recaptcha_1" style="width:320px;margin:0 auto;"></div>

  • If you notice some CSS misalignments under the ReCaptcha input:
    1. Open the file \components\com_jcomments\tpl\default\tpl_form.php (~line 130)
    2. Find the line
      $dispatcher->trigger('onInit','dynamic_recaptcha_1');
    3. With this
      $dispatcher->trigger('onInit','dynamic_recaptcha_1');
      
      // for minor css tweaks
      $document = JFactory::getDocument();
      $jcomments_recaptcha_css='.recaptchatable .recaptcha_r7_c1,.recaptchatable .recaptcha_r8_c1{top:-2px;position: relative;}';
      $document->addStyleDeclaration($jcomments_recaptcha_css);
  • An alternative to Google's ReCaptcha but modifying the same files is AreYouAHuman's PlayThru

Source(s):

Add comment


Send

Comments   

0 Tibbe Friday, 16th December 2016, 11:02 am
Hi there,

This looks like a great solution to get rid of spam comments.
How to integrate this in Joomla 3.x? Step 2 I did in settings.xml, but no result in frontend.

Kind Regards,
Tibbe
0 trake Tuesday, 31st May 2016, 2:10 pm
Still working with the last version? I cant make it work.
-1 Graham Monday, 23rd March 2015, 10:17 pm
Worked like a charm first time! :lol: Getting bored of the spam comments. This should stop them. :P
0 Fabien Wednesday, 7th May 2014, 7:05 am
Hi! Thank you for the tip, it works very well!

Fab
+3 Kman Friday, 28th March 2014, 2:33 pm
You are a rockstar mate! thanks. Followed the steps listed and it worked!

If only all tutorials were this good :P