简体   繁体   English

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

[英]How to add reCAPTCHA to Woocommerce vendors registration from

I note a number of other questions regarding WooCommerce registration, however not any concerning the WooCommerce Vendors Plugin and how vendors register on the site. 我注意到有关WooCommerce注册的许多其他问题,但是没有涉及WooCommerce供应商插件以及供应商如何在网站上注册的其他问题。

Research has allowed me to add additional fields and I have found some example reCAPTCHA code However I am missing the vital link between the two in order to get the reCAPTCHA viewable on the WooCommerce Vendors registration page. 研究使我可以添加其他字段,并且找到了一些示例reCAPTCHA 代码。但是,为了使reCAPTCHA在WooCommerce供应商注册页面上可见,我缺少两者之间的重要联系。

Is it possible to hook into wcpv_shortcode_registration_form_process by doing something like this in the example code from above? 是否可以通过在上面的示例代码中执行类似的操作来加入wcpv_shortcode_registration_form_process

/**
 * 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 );

I have indeed tried this to no avail. 我的确确实尝试过,但没有成功。

Many thanks for any help you can offer. 非常感谢您提供的任何帮助。

In order to make the captcha viewable on the vendor registration, you need to add some lines of javascript code in between the code you used to create the additional registration form. 为了使验证码在供应商注册上可见,您需要在用于创建其他注册表格的代码之间添加一些javascript代码行。

Here, just before the function to create the fields in the view, add the following code: 在此,在要在视图中创建字段的函数之前,添加以下代码:

vendors_reg_custom_field() { ?>
<script src='https://www.google.com/recaptcha/api.js' async defer></script>

Before closing the PHP function, add: 关闭PHP函数之前,添加:

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

That will make the captcha viewable. 这将使验证码可见。 Cheers. 干杯。

You can find more help here 您可以在这里找到更多帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM