ADBLOCK_MSG
Joomla: Integrate reCAPTCHA v2 (NO CAPTCHA) into JCommentsPublished: Thursday, 30 November 2017 23:29 Written by Ogri Hits: 84210
Briefly about the events that led me to the idea of the subject. I originally used kcaptcha
- the native captcha of the JComments
component. At some point, I turned this captcha off as it has annoyed commentators. Immediately a lot of spam started to appear in the comments. I started using CleanTalk
- a nominally paid (just $9 a year) plug-in that protects from spambots not only comments, but also login/registration, contacts and forms of many third-party extensions. Spam has ceased, but recently spammers began posting garbage comments, disguised as normal ones and even containing article headers in their text. CleanTalk
cannot filter them out yet. 4-5 such "comments" began to arrive every day - not very critical, and they can be easily deleted right from notifications in the mail. But still I wanted to find a compromise solution to the problem - a captcha which will block the spam not catched by CleanTalk
but at the same time not irritating potential commentators.
NO CAPTCHA reCAPTCHA
from Google (or just reCAPTCHA v2.0
) fits perfectly. Starting with Joomla 3.4, the second version is included in the reCAPTCHA
plugin that comes with the CMS. At the time of this writing, almost two years have passed since that release, but JComments
developers still do not support this plugin in their component. This does not deprecate its merits - JComments
was and remains the best free solution for comments on Joomla-site. Moreover, it's quite easy to integrate the subject into the comments on your own. To do this, you need to perform a number of simple actions.
- If your website is not registered in reCAPTCHA, go to the service page, click the Get reCAPTCHA button and add the domain name of the site. (Of course, you need to have an account with Google.) You will receive two keys - public and private. Note that if you previously used the first version of reCAPTCHA, the keys for it do not fit the second version.
- In the site administration area, open Extensions > Plugins, find the
CAPTCHA - reCAPTCHA
plugin, and click on its name. In the plug-in settings, select version 2.0, enter both keys obtained from Google, set the style and size. Enable the plugin if it's off. Save the settings. - Edit two files of the JComments component. You will need to find the specified original pieces of code and replace them with customized ones. The clickable screenshots of the comparison results in WinMerge with line numbers and highlighted changes will clearly show where and what to change (original code is on the right panel, the modified one is on the left panel).
Modification 1.
Edit the file components\com_jcomments\tpl\default\tpl_form.php. Find the following piece of code:
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 } } ?>
Replace it with:
if ($this->getVar('comments-form-captcha', 0) == 1) { $captchaEngine = 'recaptcha'; //or 'kcaptcha' if ($captchaEngine == 'recaptcha') { JPluginHelper::importPlugin('captcha'); $dispatcher = JDispatcher::getInstance(); $dispatcher->trigger('onInit','dynamic_recaptcha_1'); $recaptcha = $dispatcher->trigger('onDisplay', array(null, 'dynamic_recaptcha_1', 'class=""')); ?> <div id="comments-form-captcha-holder"> <?php echo (isset($recaptcha[0])) ? $recaptcha[0] : ''; ?> </div> <?php } else if ($captchaEngine == 'kcaptcha') { $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 } } } ?>
The comparison result:
Modification 2.
Edit the code of the file components\com_jcomments\jcomments.ajax.php. There is only one line to replace:
$captchaEngine = $config->get('captcha_engine', 'kcaptcha');
Replacement code:
$captchaEngine = 'recaptcha'; //or 'kcaptcha' if ($captchaEngine == 'recaptcha') { $post = JRequest::get('post'); JPluginHelper::importPlugin('captcha'); $dispatcher = JDispatcher::getInstance(); $result = $dispatcher->trigger('onCheckAnswer', $post['recaptcha_response_field']); $response->addScript('grecaptcha.reset();'); if (!in_array(true, $result, true)) { self::showErrorMessage(JText::_('ERROR_RECAPTCHA_V2'), 'captcha'); return $response; } } else
The comparison result:
- If the commentator forgot to click on the captcha, an error message appears. For the message text, the default is the ERROR_CAPTCHA constant from the component language file. Its value is - "Please enter security code displayed in the image!". Since picture with the code is not presented anymore, I replaced the message with a more appropriate one - "Please verify you're not a robot!" and assigned its text to a new constant ERROR_RECAPTCHA_V2. Why I decided to establish a new language constant rather than to override the value of the existing one, and how and where it should be added, is explained in the next article.
- To fit the captcha into the comment block on the page, set the style of the <div id="comments-form-captcha-holder"> element in the stylesheet file of your active template. For example, I just aligned it vertically by adding the following code to the css file of my template:
div#comments-form-captcha-holder { margin-top: 16px !important; margin-bottom: 20px !important; }
- To enable captcha output for the corresponding user groups, open the Components > JComments menu, go to Settings, and then click the Permissions tab. For the selected user groups (I recommend for ALL), check the Enable CAPTCHA checkbox. In the Layout tab, the only value in the
CAPTCHA
drop-down list is KCAPTCHA. Here it does not affect anything, the choice between the old kcaptcha and the new convenient reCAPTCHA v2.0 is implemented in the modified code. If for some reason you want to return to the native captcha, just replace the following line:$captchaEngine = 'recaptcha'; //or 'kcaptcha'
in both files with different line:
$captchaEngine = 'kcaptcha'; //or 'recaptcha'
In conclusion, let me remind you that customizations will have to be restored if new builds or versions of the JComments
component are released. But, in my opinion, this is not too much inconvenience. Firstly, it takes a few minutes to recover the changes, and secondly, the developers do not update their product too often. Nonetheless, be attentive.
Latest News
-
Tuesday, 21 July 2020 03:18
Joomla 3: Redirect to same page after successful login -
Saturday, 30 December 2017 16:11
Joomla: how to add your own language constants or override existing ones -
Thursday, 30 November 2017 23:27
Joomla: Integrate reCAPTCHA v2 (NO CAPTCHA) into JComments -
Saturday, 25 June 2016 15:33
Unified filtering of mod_jcomments_latest module's output -
Thursday, 17 September 2015 16:23
Post an illustrated Joomla-site article on Facebook using OG-tags
Articles Most Read
-
149208
Migrating from Joomla 1.5 to Joomla 2.5. Part 2-1. Transferring jDownloads and jComments -
84210
Joomla: Integrate reCAPTCHA v2 (NO CAPTCHA) into JComments -
57781
Migrating from Joomla 1.5 to Joomla 2.5. Part 2-2. Template, editor, and other extensions -
40218
Editing animated GIF-images in Photoshop CS3 -
39463
Custom 404 error page in Joomla 2.5
Login
Guest Column
Recent comments
-
Custom 404 error page in Joomla 2.5
-
Roseann
02.11.2020 08:17
Noot spamming but wanted to say this is hugely useful!
-
Roseann
02.11.2020 08:17
-
Joomla: Integrate reCAPTCHA v2 (NO CAPTCHA) into JComments
-
PG SLOT
13.01.2021 09:21
Hey! I could have sworn I've been to this site before but after browsing through some of the post ...
-
Nidia
12.12.2020 08:04
HELP. I can't write a comment without it hanging.
-
Launa
17.11.2020 15:13
I can agree with the accuracy of this as I work in a related sector. Interesting cheers for sharing.
-
JD sports vouchers
04.10.2020 03:12
Happy to pay for something like this, or donate witth paypal even
-
PG SLOT
13.01.2021 09:21
-
Joomla: how to add your own language constants or override existing ones
-
Vouchersort
29.10.2020 11:33
Is this a free template that you are using as I really love it. As a website desugner myself I hope that ...
-
Vouchersort
29.10.2020 11:33
-
Migrating from Joomla 1.5 to Joomla 2.5. Epilogue
-
Dewitt
11.02.2021 06:17
HELP. I can't write a comment without the page freezing.
-
Dewitt
11.02.2021 06:17
-
Migrating from Joomla 1.5 to Joomla 2.5. Part 2-1. Transferring jDownloads and jComments
-
Francine
14.12.2020 08:49
I cant stomach this person so sorry x
-
Vouchersort
30.10.2020 15:43
Once again, great resource for us newbs.
-
Francine
14.12.2020 08:49
-
Unified filtering of mod_jcomments_latest module's output
-
Фильмы 2022 онлайн
03.12.2021 21:37
This web site definitely has all of the info I wanted about this subject and didn't know who to ask.
-
Фильмы 2022 онлайн
03.12.2021 21:37
Comments
You have touched some fastidious points here. Any way keep up wrinting.
I amm going to convey her.
シアリスジェネリック比較 ダポキセチン副作用 kokyn.net
at web, except I know I am getting know-how daily by reading such good
articles.
to be available that in detail, thus that
thing is maintained over here.
web site, as here every information is quality based material.
Simple but very accurate info… Thank you for sharing this one.
A must read article!
this wonderful read!! I definitely liked every little bit of it and i also have you
bookmarked to look at new stuff in your website.
tutoringand fully defined, keep it up all the time.