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.
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);
See this: https://formidableforms.com/knowledgebase/user-registration/add-login-form-to-your-site/
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!
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
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.
In that case, I recommend using WPMU's defender, which has support for Captcha.
Please login or Register to submit your answer