2

So far I have completed adding google recaptcha on contact page, login page, and pages that has forms on the front end, all are working great as expected.

now only one page is remaining for me to add google recaptcha that is when user clicks on lost your password? link, so I tried adding:

add_action('lost_password', 'my_reset_password');

with the above action added I can see the google captcha on top left corner at the generate new password page, but I want it below the username and email field.

and after that how can I validate the google recaptcha when user clicks on generate new password button. I am new to wordpress, please help. thanks in advance.

I am using latest WordPress version,

~UPDATE~

(now tried the below, but its not validating my captcha, and sending the email with reset link)

    add_action('lostpassword_form', 'my_reset_password_field');

    function my_reset_password_field() {
        ?>
        <script src='https://www.google.com/recaptcha/api.js'></script>
        <p>
            <label for="google-captcha">Verify, you are human?</label>
        <div style="overflow: hidden" class="g-recaptcha" data-sitekey="asdaasdasdasdasdasdasdasdasd"></div>
        </p>

        <br>
        <?php
    }

    add_action('lostpassword_post', 'validate_lost_password');

    function validate_lost_password() {
        if (isset($_POST['g-recaptcha-response'])) {
            $array = array('response' => $_POST['g-recaptcha-response'], 'userip' => $_SERVER['REMOTE_ADDR'],
                'secret' => '456456gdfgdfgertegdfgdfg');
            $result = gcaptcha($array);
            if (!$result) {
                global $errors;
                $errors->add('invalid', '<strong>ERROR</strong>: Verification Failed.');
            }
        } 
    }
2

got a working solution,

but wordpress first validate the username or email address then my validation takes place..

I have used allow_password_reset filter instead of lostpassword_post action:

        add_filter( 'allow_password_reset', 'my_password_reset_helper' );

        function my_password_reset_helper($true)
        {

            if (isset($_POST['g-recaptcha-response'])) {
                $array = array('response' => $_POST['g-recaptcha-response'], 
                               'userip' => $_SERVER['REMOTE_ADDR'],
                               'secret' => 'asddfer345gfdg4veg45y34635345');
                $result = gcaptcha($array);
                if (!$result) {
                    return new WP_Error('denied', 
                               '<strong>ERROR</strong>: Verification Failed.');
                }
                return $true;
            }
            return $true;

        } 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.