How to use Joomla recaptcha plugin with my custom module?

21,771

Solution 1

In order to use joomla default recaptcha plugin follow these steps-

1)Get recaptcha keys from http://www.google.com/recaptcha

2)Set these keys to recaptcha plugin and activate it if it's not.

3)Put below code where you want to show recaptcha

//php code
JPluginHelper::importPlugin('captcha');
$dispatcher = JDispatcher::getInstance();
$dispatcher->trigger('onInit','dynamic_recaptcha_1');

//html code inside form tag
<div id="dynamic_recaptcha_1"></div>

4)Put this code where you validating/processing the form

$post = JRequest::get('post');      
JPluginHelper::importPlugin('captcha');
$dispatcher = JDispatcher::getInstance();
$res = $dispatcher->trigger('onCheckAnswer',$post['recaptcha_response_field']);
if(!$res[0]){
    die('Invalid Captcha');
}

//For Joomla 3.x

$post = JFactory::getApplication()->input->post;
$dispatcher = JEventDispatcher::getInstance();

Solution 2

Following up on Irfan's code, additionally I had to do the following for the captcha to show up:

Add the following to the template code.

JHtml::_('behavior.keepalive');

Apparently it includes the mootools library.

It seems that an event domready is added, but the event only fires when mootools library is used. So just check your html source and see if mootools is being used. I might be completely wrong but hope my solution helps someone.

Share:
21,771
Toretto
Author by

Toretto

I am Sr. Software Engineer, I have 4+ year of experience. I love to work with PHP ,Joomla, Drupal, WordPress.

Updated on July 17, 2022

Comments

  • Toretto
    Toretto almost 2 years

    I have created a custom module for my contactus form. Now I want to use Joomla recaptcha plugin with this module.

    Any idea how to get this done?