wordpress 在提交按钮之前添加recaptcha v2到woocommerce产品评论评论

o8x7eapl  于 2022-11-28  发布在  WordPress
关注(0)|答案(1)|浏览(169)

我想添加谷歌recaptcha v2到woocommerce产品页面完全在提交按钮之前的评论区后,我发现了一个和平的代码,在帖子中工作得很好,但我不能让它在woocommerce产品评论评论工作,这是我有:
这部分在single.php中位于get_header()之前;

wp_enqueue_script('google-recaptcha', 'https://www.google.com/recaptcha/api.js');

这部分在函数中。php:

/**
 * Google recaptcha add before the submit button
 */
function add_google_recaptcha($submit_field) {
    $submit_field['submit_field'] = '<div class="g-recaptcha" data-sitekey="your_site_key"></div><br>' . $submit_field['submit_field'];
    return $submit_field;
}
if (!is_user_logged_in()) {
    add_filter('comment_form_defaults','add_google_recaptcha');
}
 
/**
 * Google recaptcha check, validate and catch the spammer
 */
function is_valid_captcha($captcha) {
$captcha_postdata = http_build_query(array(
                            'secret' => 'your_secret_key',
                            'response' => $captcha,
                            'remoteip' => $_SERVER['REMOTE_ADDR']));
$captcha_opts = array('http' => array(
                      'method'  => 'POST',
                      'header'  => 'Content-type: application/x-www-form-urlencoded',
                      'content' => $captcha_postdata));
$captcha_context  = stream_context_create($captcha_opts);
$captcha_response = json_decode(file_get_contents("https://www.google.com/recaptcha/api/siteverify" , false , $captcha_context), true);
if ($captcha_response['success'])
    return true;
else
    return false;
}
 
function verify_google_recaptcha() {
$recaptcha = $_POST['g-recaptcha-response'];
if (empty($recaptcha))
    wp_die( __("<b>ERROR:</b> please select <b>I'm not a robot!</b><p><a href='javascript:history.back()'>« Back</a></p>"));
else if (!is_valid_captcha($recaptcha))
    wp_die( __("<b>Go away SPAMMER!</b>"));
}
if (!is_user_logged_in()) {
    add_action('pre_comment_on_post', 'verify_google_recaptcha');
}

知道怎么用吗?

wnavrhmk

wnavrhmk1#

您好尝试这个并添加儿童主题function.php

/*
 * Add reCaptcha on Product Review
 */
function ssky_google_recaptcha_field_html() {
    ob_start();
    ?>
    <div class="ssky-recaptcha-container">
        <div class="ssky-recaptcha-field-container">
            <div class="g-recaptcha" data-sitekey="your_site_key"></div>
        </div>
    </div>
    <?php
    wp_enqueue_script('google-recaptcha', 'https://www.google.com/recaptcha/api.js');
    return ob_get_clean();
}

add_filter( 'comment_form_defaults', 'ssky_google_recaptcha_field', 10, 1 );
function ssky_google_recaptcha_field( $defaults ) {
    if ( ! is_product() || is_user_logged_in() ) {
        return;
    }
    $defaults['submit_button'] .= ssky_google_recaptcha_field_html();
    return $defaults;
}

/**
 * Google recaptcha check, validate and catch the spammer
 */
function is_valid_captcha($captcha) {
    $captcha_postdata = http_build_query(array(
        'secret' => 'your_secret_key',
        'response' => $captcha,
        'remoteip' => $_SERVER['REMOTE_ADDR']));
    $captcha_opts = array('http' => array(
        'method'  => 'POST',
        'header'  => 'Content-type: application/x-www-form-urlencoded',
        'content' => $captcha_postdata));
    $captcha_context  = stream_context_create($captcha_opts);
    $captcha_response = json_decode(file_get_contents("https://www.google.com/recaptcha/api/siteverify" ,false,                    $captcha_context), true);
if ($captcha_response['success'])
    return true;
else
    return false;
}

function verify_google_recaptcha() {
    $recaptcha = $_POST['g-recaptcha-response'];
    if (empty($recaptcha))
        wp_die( __("<b>ERROR:</b> please select <b>I'm not a robot!</b><p><a href='javascript:history.back()'>« Back</a></p>"));
    else if (!is_valid_captcha($recaptcha))
        wp_die( __("<b>Go away SPAMMER!</b>"));
}

if (!is_user_logged_in()) {
    add_action('pre_comment_on_post', 'verify_google_recaptcha');
}

相关问题