Php 如何从添加reCAPTCHA到Woocommerce供应商注册

Php 如何从添加reCAPTCHA到Woocommerce供应商注册,php,wordpress,woocommerce,Php,Wordpress,Woocommerce,我注意到一些关于WooCommerce注册的其他问题,但是没有任何关于WooCommerce供应商插件以及供应商如何在网站上注册的问题 允许我添加其他字段,并且我找到了一些示例reCAPTCHA,但是为了在WooCommerce供应商注册页面上查看reCAPTCHA,我缺少了两者之间的重要链接 通过在上面的示例代码中执行类似的操作,是否可以连接到wcpv\u快捷码\u注册\u表单\u流程 /** * Add reCapcha to the Vendor registration page

我注意到一些关于WooCommerce注册的其他问题,但是没有任何关于WooCommerce供应商插件以及供应商如何在网站上注册的问题

允许我添加其他字段,并且我找到了一些示例reCAPTCHA,但是为了在WooCommerce供应商注册页面上查看reCAPTCHA,我缺少了两者之间的重要链接

通过在上面的示例代码中执行类似的操作,是否可以连接到
wcpv\u快捷码\u注册\u表单\u流程

/**
 * Add reCapcha to the Vendor registration page 
 */

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( 'wcpv_shortcode_registration_form_process', 'wooc_validate_re_captcha_field', 10, 3 );
/**
*将reCapcha添加到供应商注册页面
*/
函数wooc\u validate\u re\u captcha\u字段($username、$email、$wpErrors)
{
$remoteIP=$\u服务器['REMOTE\u ADDR'];
$recaptchaResponse=$_POST['g-recaptcha-response'];
$response=wp\u remote\u post($response)https://www.google.com/recaptcha/api/siteverify', [
“正文”=>[
“机密”=>“这里是私钥!!!”,
“响应”=>$recaptchaResponse,
“remoteip”=>$remoteip
]
] );
$response\u code=wp\u remote\u retrieve\u response\u code($response);
$response\u body=wp\u remote\u retrieve\u body($response);
如果($response_code==200)
{
$result=json\u decode($response\u body,true);
如果(!$result['success'])
{
开关($result['error-code']))
{
案例“缺少输入机密”:
案例“无效输入密码”:
$wpErrors->add('recaptcha',uuu('错误:无效的recaptcha密钥','woocommerce');
打破
案例“缺少输入响应”:
案例“无效输入响应”:
$wprerrors->add('recaptcha',uuuu('ERROR:请勾选此框以证明您不是机器人。','woocommerce');
打破
违约:
$wpErrors->add('recaptcha',uuuu('错误:验证recaptcha时出错。','woocommerce');
打破
}
}
}
其他的
{
$wpErrors->add('recaptcha_error',uuu('error:无法访问recaptcha服务器','woocommerce');
}
}
添加行动('wcpv\u快捷码\u注册\u表格\u流程','wooc\u验证\u re\u验证码\u字段',10,3);
我确实试过,但没有用


非常感谢你能提供的任何帮助

为了使验证码在供应商注册时可见,您需要在用于创建附加注册表单的代码之间添加一些javascript代码行

在此,在创建视图中字段的函数之前,添加以下代码:

vendors_reg_custom_field() { ?>
<script src='https://www.google.com/recaptcha/api.js' async defer></script>
vendors\u reg\u custom\u field(){?>
在关闭PHP函数之前,添加:

<p class="form-row form-row-wide">
<div class="g-recaptcha" data-sitekey="YOUR-RECAPTCHA-SITE-KEY-HERE</div>
</p>
<?php 
}