I’m trying to add ReCaptcha to my login page. I know there are many many plugins by per request I cannot use plugins. So far so good, I’m able to add the Captcha to my login but unable to show errors.
It will error out on me when I enter the wrong password, but if I enter the right password and do not enter the Captcha I get no errors (which I should get a Captcha not filled error). So the only time my captcha_errors
function fires is when user name / password is wrong.
How do I get my login form to check the ReCaptcha before checking the login credentials?
do_action('login_head');
// this function adds captcha to the login form
function addCaptcha() {
if( session_id() == "" )
@session_start();
if (isset( $_SESSION["recaptcha_response_field"] ))
unset( $_SESSION["recaptcha_response_field"] );
?>
<p class="cptch_block">
<div id="cap"></div>
<script type='text/javascript' src='http://www.google.com/recaptcha/api/js/recaptcha_ajax.js'></script>
<script type='text/javascript'>
Recaptcha.create("pubk", "cap", {theme:"clean", callback: Recaptcha.focus_response_field});
</script>
<noscript>
<iframe src='http://www.google.com/recaptcha/api/noscript?k=pubk' height='300' width='500'></iframe>
<br />
<textarea name='recaptcha_challenge_field' rows='3' cols='40'></textarea>
<input type='hidden' value='manual_challenge' name='recaptcha_response_field'/>
</noscript>
</p>
<br />
<?php
return true;
}
function verifyCaptcha($pageid) {
$result = false;
if( session_id() == "" )
@session_start();
$pvk = 'prvk';
if (!empty($pvk) && isset($_POST['recaptcha_response_field'])) {
if(!function_exists('recaptcha_check_answer')) {
require_once 'recaptchalib.php';
}
$resp = recaptcha_check_answer($pvk, $_SERVER['REMOTE_ADDR'], $_POST['recaptcha_challenge_field'], $_POST['recaptcha_response_field']);
$result = $resp->is_valid;
}
if(!$result)
return $_SERVER["REQUEST_URI"];
else
return admin_url();
}
// this function checks captcha posted with a login
function captcha_errors( $errors ) {
if ( isset( $_SESSION['cptch_error'] ) )
unset( $_SESSION['cptch_error'] );
// return $errros.'<strong>TEST</strong>';
if ("" == $_REQUEST['recaptcha_response_field'] ) {
return $errors.'<strong>'. __( 'ERROR', 'captcha' ) .'</strong>: '. __( 'Please fill out The Cpatcha.', 'captcha' );
}
if ( isset( $_REQUEST['recaptcha_response_field'] ) && verifyCaptcha()) {
// captcha was matched
} else {
return $errors.'<strong>'. __( 'ERROR', 'captcha' ) .'</strong>: '. __( 'Please enter a valid CAPTCHA value.', 'captcha' );
}
return( $errors );
}
add_action( 'login_form', 'addCaptcha' );
add_filter( 'login_redirect', 'verifyCaptcha', 10, 3 );
add_filter( 'login_errors', captcha_errors);
Use this plugin for this purpose SI Captcha