Woocommerce-如何添加新的Google“ no Captcha” reCaptcha进入前端注册表格 [英] Woocommerce - How to add the new Google "no Captcha" reCaptcha into frontend registration form

查看:106
本文介绍了Woocommerce-如何添加新的Google“ no Captcha” reCaptcha进入前端注册表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在前端Woocommerce注册表单页面中使用新的Google no Captcha reCaptcha。

I need to use the new Google "no Captcha" reCaptcha in frontend Woocommerce registration form page.

我在<之前插入了以下代码/ head>标记:

I've inserted following code before the < / head > tag:

<script src='https://www.google.com/recaptcha/api.js'></script>

以及文件 woocommerce\myaccount-form-login.php中的一个内部表单标签:

and this one inside form tab in file "woocommerce\myaccount\form-login.php":

<div class="g-recaptcha" data-sitekey="xxxxxxxxx MY ID xxxxxxxxx"></div>

其中,显然, xxxxxxxxx MY ID xxxxxxxxx是我的Google ID代码。

where, obviously, "xxxxxxxxx MY ID xxxxxxxxx" is my google id code.

它显示了新的验证码框,但是如果我尝试注册新用户而不检查验证码,它不会停止注册过程并完成注册而不会出错。

It shows the new captcha box but if I try to register a new user without check the captcha, it doesn't stops registration process and complete registration without errors.

推荐答案

自从Stimart打开这个问题已有一段时间了,但是如果有人需要,我会在这里提出我的建议。

It's being a while since Stimart opened this question but I'll give here my suggestion in case someone needed it.

Stimart在这里需要添加一些PHP代码来验证当用户在reCaptcha框上打钩以确认自己不是机器人时Google发送到页面的reCaptcha响应值。

Stimart here need to add some PHP code to validate the reCaptcha response value that google sends to the page when the user ticks the reCaptcha box to confirm they are not a robot.

最困难的方法是在functions.php文件中编写如下内容:

The hard way would be to write something like this in the functions.php file:

function wooc_validate_re_captcha_field( $username, $email, $wpErrors )
{
    $remoteIP = $_SERVER['REMOTE_ADDR'];
    $recaptchaResponse = $_POST['g-recaptcha-response'];

    $response = wp_remote_post( 'https://www.google.com/recaptcha/api/siteverify', [
        'body' => [
            'secret'   => 'PRIVATE KEY HERE !!!',
            'response' => $recaptchaResponse,
            'remoteip' => $remoteIP
        ]
    ] );

    $response_code = wp_remote_retrieve_response_code( $response );
    $response_body = wp_remote_retrieve_body( $response );

    if ( $response_code == 200 )
    {
        $result = json_decode( $response_body, true );

        if ( ! $result['success'] )
        {
            switch ( $result['error-codes'] )
            {
                case 'missing-input-secret':
                case 'invalid-input-secret':
                    $wpErrors->add( 'recaptcha', __( '<strong>ERROR</strong>: Invalid reCAPTCHA secret key.', 'woocommerce' ) );
                    break;

                case 'missing-input-response' :
                case 'invalid-input-response' :
                    $wpErrors->add( 'recaptcha', __( '<strong>ERROR</strong>: Please check the box to prove that you are not a robot.', 'woocommerce' ) );
                    break;

                default:
                    $wpErrors->add( 'recaptcha', __( '<strong>ERROR</strong>: Something went wront validating the reCAPTCHA.', 'woocommerce' ) );
                    break;
            }
        }
    }
    else
    {
        $wpErrors->add( 'recaptcha_error', __( '<strong>Error</strong>: Unable to reach the reCAPTCHA server.', 'woocommerce' ) );
    }
}
add_action( 'woocommerce_register_post', 'wooc_validate_re_captcha_field', 10, 3 );

您可以查看此文章 http://www.themelocation.com/how-to-add-custom-fields-to- user-registration-form-in-woocommerce / 以及此插件的方式 https:/ /wordpress.org/plugins/theme-my-login/ 处理此问题。

You can check this article http://www.themelocation.com/how-to-add-custom-fields-to-user-registration-form-in-woocommerce/ and the way this plugin https://wordpress.org/plugins/theme-my-login/ handle this problem.

容易得多是要安装的并设置一个类似于的插件。 ;)

or a lot easier is to install and set up a plugin like this one. ;)

希望有帮助。 :)

这篇关于Woocommerce-如何添加新的Google“ no Captcha” reCaptcha进入前端注册表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆