Joomla: Integrate reCAPTCHA v2 (NO CAPTCHA) into JCommentsPublished: Thursday, 30 November 2017 23:29 Written by Ogri Hits: 74221
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
-
142505
Migrating from Joomla 1.5 to Joomla 2.5. Part 2-1. Transferring jDownloads and jComments -
74221
Joomla: Integrate reCAPTCHA v2 (NO CAPTCHA) into JComments -
44156
Migrating from Joomla 1.5 to Joomla 2.5. Part 2-2. Template, editor, and other extensions -
38858
Editing animated GIF-images in Photoshop CS3 -
36292
Migrating from Joomla 1.5 to Joomla 2.5. Part 1. Transferring content
Login
Guest Column
Recent comments
-
Custom 404 error page in Joomla 2.5
-
fafaslot 10.06.2023 05:59
-
-
Joomla 2.5: Remove breadcrumbs from specific pages
-
รับทำเว็บพนันออนไลน์ 09.06.2023 22:58
An intriguing discussion is definitely worth comment. I think that you ought to publish more about ...
-
-
Joomla: Integrate reCAPTCHA v2 (NO CAPTCHA) into JComments
-
Going Here 10.06.2023 02:46
Thank you for the auspicious writeup. It actually was a leisure account it. Look complicated to ...
-
xayalmuazzin.com 09.06.2023 23:04
Нi! This is my 1st comment here so I just wanted to give а quick shout out annd say I truly еnjoy ...
-
диплом института 15.05.2021 03:19
Fantastic items from you, man. I've be aware your stuff prior to and you're simply extremely magnificent.
-
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 ...
-
-
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.
-
-
Migrating from Joomla 1.5 to Joomla 2.5. Part 2-1. Transferring jDownloads and jComments
-
calendrier mai 09.06.2023 13:43
These are actually enormous ideas in regarding blogging. You have touched some fastidious points here.
-
-
The structure of internal links within Joomla site
-
918kiss sg 10.06.2023 06:28
Get ready for the ultimate gaming experience with 918kiss. Spin and win on classic casino favorites such ...
-
-
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.
-
Read more...