Search
 
SCRIPT & CODE EXAMPLE
 

PHP

how to add recaptcha to woocommerce register php

// Add field into the registration form

    function nada_woocommerce_edit_registration_form() {
            ?>
            <p id="recaptcha" class="g-recaptcha" data-sitekey="##your-google-captcha-key##"></p>
            <?php
        }
        add_action( 'woocommerce_register_form', 'nada_woocommerce_edit_registration_form', 15 );

    /**
     * Validate Woocommerce Registration form fields
     */

    function nada_validate_extra_register_fields( $errors, $username, $email ) {
        if ( empty( $_POST['g-recaptcha-response'] ) ) {
                $errors->add( 'captcha-error', wp_kses_post( '<strong>Error</strong>: Captcha is missing.', 'nada' ) );
        }
        return $errors;
    }
    add_filter( 'woocommerce_registration_errors', 'nada_validate_extra_register_fields', 10, 3 );
Comment

how to add recaptcha to woocommerce register php

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 );
Comment

PREVIOUS NEXT
Code Example
Php :: php isset ternary operator 
Php :: php insert hyphen into spaces in string 
Php :: multiply a string php 
Php :: laravel log to console 
Php :: name csrf token laravel mismatch 
Php :: get html file data to variable in php 
Php :: deactivate plugin wp cli 
Php :: php json_encode 
Php :: laravel json search 
Php :: laravel find by 
Php :: magento 2 print php error 
Php :: Laravel Boot strap Pagination 
Php :: showing php code in browser 
Php :: how to add attributes to an exsisting object in php 
Php :: php convert to lowercase 
Php :: laravel group routes 
Php :: pass javascriot value from one page to another 
Php :: php get url 
Php :: how to use multiple permission in blade 
Php :: force delete soft delete laravel 
Php :: laravel 404 
Php :: PHP Create a MySQL Database 
Php :: convert byte to megabyte php 
Php :: php check if input is int 
Php :: password_hash 
Php :: model json laravel accessor to convert to array 
Php :: redirection in php 
Php :: php random number generator 
Php :: laravel error storage permission denied 
Php :: laravel get auth user id 
ADD CONTENT
Topic
Content
Source link
Name
2+9 =