Skip to content

Instantly share code, notes, and snippets.

@tdmrhn
Last active July 16, 2021 09:14
Show Gist options
  • Save tdmrhn/58de855fbe8ca0b81e6d0be0d7ebce66 to your computer and use it in GitHub Desktop.
Save tdmrhn/58de855fbe8ca0b81e6d0be0d7ebce66 to your computer and use it in GitHub Desktop.
Custom Recaptcha v1
<?php
add_action('login_enqueue_scripts', 'login_recaptcha_script');
function login_recaptcha_script() {
wp_register_script('recaptcha_login', 'https://www.google.com/recaptcha/api.js?render=reCAPTCHA_site_key');
wp_enqueue_script('recaptcha_login');
}
add_action( 'login_form', 'display_recaptcha_on_login' );
function display_recaptcha_on_login() {
echo "<script> function onSubmit(token) { document.getElementById('loginform').submit(); } </script> <button class='g-recaptcha' data-sitekey='YOUR_PUBLIC_KEY' data-callback='onSubmit' data-size='invisible' style='display:none;'>Submit</button>";
}
add_filter('wp_authenticate_user', 'verify_recaptcha_on_login', 10, 2);
function verify_recaptcha_on_login($user, $password) {
if (isset($_POST['g-recaptcha-response'])) {
$response = wp_remote_get( 'https://www.google.com/recaptcha/api/siteverify?secret=YOUR_SECRET_KEYx&response=' . $_POST['g-recaptcha-response'] );
$response = json_decode($response['body'], true);
if (true == $response['success']) {
return $user;
} else {
return new WP_Error( 'Captcha Invalid', __('<strong>ERROR</strong>: You are a bot') );
}
} else {
return new WP_Error( 'Captcha Invalid', __('<strong>ERROR</strong>: You are a bot. If not then enable JavaScript.') );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment