CAPTCHA on Login Page

By: Rudi JvR | Asked: 11/05/2024
ForumsCategory: General questionsCAPTCHA on Login Page
Rudi JvR asked 5 months ago

I have a Login/Logout page on my site which uses the [frm-login show_lost_password="1"] shortcode for the login form fields.

How do I add a CAPTCHA field to this page?

Thanks in advance.

3 Answers
Best Answer
Armo Txa answered 1 month ago

you can use in the Code Snippets plugin or add directly to the functions.php file of your WordPress themeComplete Code for Adding Cloudflare Turnstile CAPTCHA to Login and Lost Password Pages: // Function to retrieve Cloudflare Turnstile keysfunction cloudflare_key() {    $sitekey = "YOURKEY";  // Replace with your own site key    $secretkey = "YOURSKEY";  // Replace with your own secret key    return [$sitekey, $secretkey];} // Enqueue Cloudflare Turnstile script and add custom styles to the login pageadd_action('login_enqueue_scripts', function() {    wp_enqueue_script('cloudflare-turnstile', 'https://challenges.cloudflare.com/turnstile/v0/api.js');    echo "<style>p.submit, p.forgetmenot {margin-top: 10px!important;}.login form{width: 303px;} div#login_error {width: 322px;}</style>";}); // Add Cloudflare Turnstile widget to the login formadd_action('login_form', function() {    echo '<div class="cf-turnstile" data-sitekey="' . cloudflare_key()[0] . '"></div>';}); // Verify CAPTCHA on loginadd_action('wp_authenticate_user', function($user, $password) {    // Get the CAPTCHA response from the form    $captcha = $_POST['cf-turnstile-response'];     // If no CAPTCHA response is provided, return an error    if (!$captcha) {        return new WP_Error('captcha_invalid', __('<center>Captcha Invalid! Please check the captcha!</center>'));    }     // Get the secret key and user IP address    $secretKey = cloudflare_key()[1];    $ip = $_SERVER['REMOTE_ADDR'];     // Prepare data for the Cloudflare Turnstile API verification    $url_path = 'https://challenges.cloudflare.com/turnstile/v0/siteverify';    $data = array(        'secret' => $secretKey,        'response' => $captcha,        'remoteip' => $ip    );     // Set up the HTTP POST request    $options = array(        'http' => array(            'method' => 'POST',            'content' => http_build_query($data)        )    );        // Execute the API request    $stream = stream_context_create($options);    $result = file_get_contents($url_path, false, $stream);     // If the request fails, return an error    if ($result === false) {        return new WP_Error('captcha_invalid', __('<center>Captcha Invalid! Please check the captcha!</center>'));    }     // Decode the JSON response from the API    $responseKeys = json_decode($result, true);     // If the CAPTCHA validation fails, return an error    if (intval($responseKeys["success"]) !== 1) {        return new WP_Error('captcha_invalid', __('<center>Captcha Invalid! Please check the captcha!</center>'));    }     // CAPTCHA is valid, continue the login process    return $user; }, 10, 2); // Enqueue Turnstile script for lost password pageadd_action("wp_head", function() {    wp_enqueue_script('cloudflare-turnstile', 'https://challenges.cloudflare.com/turnstile/v0/api.js');}); // Add custom styles to the lost password pagefunction lost_password_style() {    wp_register_script('lostpassword-recaptcha', 'https://challenges.cloudflare.com/turnstile/v0/api.js', false, NULL);    wp_enqueue_script('lostpassword-recaptcha');    echo "<style>p.submit {margin-top: 10px!important;}</style>";}add_action('login_enqueue_scripts', 'lost_password_style'); // Add Cloudflare Turnstile widget to the lost password formadd_action('lostpassword_form', function() {    echo '<div class="cf-turnstile" data-sitekey="' . cloudflare_key()[0] . '"></div>';}); // Verify CAPTCHA during password reset (lost password)add_action('lostpassword_post', function($errors) {    // Get the CAPTCHA response from the form    $captcha = $_POST['cf-turnstile-response'];     // If no CAPTCHA response is provided, add an error and stop the process    if (empty($captcha)) {        $errors->add('captcha_invalid', __('Captcha is missing. Please check the captcha!'));        return $errors;  // Stop further processing    }     // Get the secret key and user IP address    $secretKey = cloudflare_key()[1];    $ip = $_SERVER['REMOTE_ADDR'];     // Prepare data for the Cloudflare Turnstile API verification    $url_path = 'https://challenges.cloudflare.com/turnstile/v0/siteverify';    $data = array(        'secret' => $secretKey,        'response' => $captcha,        'remoteip' => $ip    );     // Set up the HTTP POST request    $options = array(        'http' => array(            'method' => 'POST',            'content' => http_build_query($data)        )    );     // Execute the API request    $stream = stream_context_create($options);    $result = file_get_contents($url_path, false, $stream);     // If the request fails, return an error    if ($result === false) {        $errors->add('captcha_invalid', __('Failed to connect to Cloudflare Turnstile service.'));        return $errors;    }     // Decode the JSON response from the API    $responseKeys = json_decode($result, true);     // If the CAPTCHA validation fails, return an error    if (intval($responseKeys["success"]) !== 1) {        $errors->add('captcha_invalid', __('Captcha Invalid! Please check the captcha!'));        return $errors;    }     // CAPTCHA is valid, continue with the password reset process    return;}, 10, 1); 

Victor Font Staff answered 5 months ago

See this: https://formidableforms.com/knowledgebase/user-registration/add-login-form-to-your-site/

Rudi JvR replied 5 months ago

Thanks Victor. I did read this documentation, but I was hoping I was missing something. Unfortunately it seems the only way to get captcha to work with [from-login] is to install another separate plugin.

Thanks again!

Bobby Clapp Staff answered 4 months ago

This is built in. No plugin is required. https://formidableforms.com/knowledgebase/recaptcha/Login is referenced here: https://formidableforms.com/knowledgebase/recaptcha/#kb-google-recaptcha-verification-failed

Rudi JvR replied 3 months ago

Thanks Bobby.

But captcha actually doesn't work with the login form [frm-login]

The Knowledge base says that captcha is not supported for the [frm-login] form.
https://formidableforms.com/knowledgebase/user-registration/add-login-form-to-your-site/#kb-add-recaptcha-to-login-page

"However, there isn't yet a way to add a recaptcha with Formidable Forms."

A bit unfortunate, as site security, especially LOGIN forms is where captcha is suppose to help.

Bobby Clapp Staff replied 3 months ago

In that case, I recommend using WPMU's defender, which has support for Captcha.

Making the Best WordPress Plugin even better - Together

Take on bigger projects with confidence knowing you have access to an entire community of Formidable Experts and Professionals who have your back when the going gets tough. You got this!
Join the community
crossarrow-right