drpogkqqi536984960 2017-06-26 09:00
浏览 56

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

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.

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.

Is it possible to hook into wcpv_shortcode_registration_form_process by doing something like this in the example code from above?

/**
 * 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.

  • 写回答

1条回答 默认 最新

  • drs3925 2017-12-18 13:56
    关注

    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.

    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:

    <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

    评论

报告相同问题?

悬赏问题

  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?