php - 在提交按钮之前将 recaptcha v2 添加到 woocommerce 产品评论评论
问题描述
我想在提交按钮之前的评论区域之后将 google recaptcha v2 添加到 woocommerce 产品页面,我发现在帖子中可以正常工作的代码和平,但我不能让它在 woocommerce 产品评论评论中工作,这就是我所拥有的:
这部分在 get_header() 之前的 single.php 中;
wp_enqueue_script('google-recaptcha', 'https://www.google.com/recaptcha/api.js');
这部分在functions.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');
}
知道如何使它工作吗?
解决方案
推荐阅读
- python-3.x - 根据 Dataframe pandas 中的列过滤器获取 n 行
- python - Jinja2 解析 JSON 对象,无法从中获取值
- android - 在使用它们声明依赖项时,Kapt 和 Gradle 中的实现有什么区别?
- entity-framework-core - EF Core 级联删除可为空的外键
- android - 宽度受限的Textview有时在recyclerview中不起作用
- reactjs - 我应该在哪个 AWS 服务中部署我的 react 应用程序
- angular - 如何使用 ngClass 将多个类应用于 div?
- java - Spring Container 和 BeanFactory 的实现之间的关系?
- nsis - NSIS 问题:当“.onInstSuccess”功能未完成时,我可以禁用“MUI_PAGE_FINISH”页面中的“完成”按钮吗?
- django - Django 通道 2 和信号:Websocket 连接失败