Code: Select all
J
JPluginHelper::importPlugin('captcha');
$dispatcher = JDispatcher::getInstance();
$dispatcher->trigger('onInit');
Moderators: ooffick, General Support Moderators
Code: Select all
J
JPluginHelper::importPlugin('captcha');
$dispatcher = JDispatcher::getInstance();
$dispatcher->trigger('onInit');
Code: Select all
use Joomla\CMS\Plugin\EventDispatcher;
$dispatcher = EventDispatcher::getInstance();
$dispatcher->trigger('onInit', ['captcha']);
Code: Select all
use Joomla\CMS\Captcha\Captcha;
$captcha = Captcha::getInstance($type);
// To display captcha.
$captcha->display($name, $id);
// To validate answer.
$captcha->checkAnswer($answer);
Hello. I have recently tried to refactor some of my old Joomla! 3-era captcha code to the new ways of J! 4 and 5. Since the stock Joomla! reCAPTCHA plugins have all been deprecated in J!5, I've decided to switch to what appears to be your Google reCAPTCHA v3 plugin from here: https://github.com/SharkyKZ/joomla-recaptcha-v3-pluginSharkyKZ wrote: ↑Tue Jul 18, 2023 5:25 amDon't use event dispatcher. That's wrong even in J3. Use Joomla\CMS\Captcha\Captcha class.
Code: Select all
use Joomla\CMS\Captcha\Captcha; $captcha = Captcha::getInstance($type); // To display captcha. $captcha->display($name, $id); // To validate answer. $captcha->checkAnswer($answer);
Code: Select all
use Joomla\CMS\Captcha\Captcha;
use Joomla\CMS\Factory;
$joomlaApplication = Factory::getApplication();
if (Factory::getUser()->guest && (Factory::getConfig()->get('captcha') === 'recaptcha_v3')) {
$joomlaCaptcha = Captcha::getInstance('recaptcha_v3');
try {
echo $joomlaCaptcha->display('custom-form-captcha', 'custom-form-captcha-123');
} catch (Exception $joomlaCaptchaException) {
// In case the captcha initialization process fails
$joomlaApplication->enqueueMessage(
$joomlaCaptchaException->getMessage(),
$joomlaApplication::MSG_ERROR,
);
}
}
Code: Select all
use Joomla\CMS\Captcha\Captcha;
use Joomla\CMS\Factory;
$joomlaApplication = Factory::getApplication();
if (Factory::getUser()->guest && (Factory::getConfig()->get('captcha') === 'recaptcha_v3')) {
$joomlaCaptcha = Captcha::getInstance('recaptcha_v3');
$joomlaCaptchaResult = false;
try {
$joomlaCaptchaResult = $joomlaCaptcha->checkAnswer(
$joomlaApplication->input->post->get(
'custom-form-captcha',
'missing-input-response', // Default reCAPTCHA error code
'string',
),
);
} catch (Exception $joomlaCaptchaException) {
// Display any captcha issues to the user
$joomlaApplication->enqueueMessage(
$joomlaCaptchaException->getMessage(),
$joomlaApplication::MSG_WARNING,
);
}
if ($joomlaCaptchaResult === false) {
// cancel form submission...
}
}
Code: Select all
$joomlaCaptcha->checkAnswer($_POST['custom-form-captcha']);
Code: Select all
$joomlaCaptcha->checkAnswer('custom-form-captcha');
Code: Select all
$joomlaCaptcha->checkAnswer(''); // This is supposed to activate a fallback to the older 'g-recaptcha-response' input that reCAPTCHA v3 still supports