我按照here给出的教程。
我在google_recaptcha.php
主题文件夹中添加了/inc
个文件
<?php
$data;
header('Content-Type: application/json');
error_reporting(E_ALL ^ E_NOTICE);
if(isset($_POST['g-recaptcha-response'])){
$captcha=$_POST['g-recaptcha-response'];
}
if(!$captcha){
$data=array('nocaptcha' => 'true');
echo json_encode($data);
exit;
}
$google_secret_recaptcha = get_option('recaptcha_secret_key');
$response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$google_secret_recaptcha."&response=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']);
if($response.success==false) {
$data=array('spam' => 'true');
echo json_encode($data);
} else {
$data=array('spam' => 'false');
echo json_encode($data);
}
我之所以这样做,以便我可以将wp后端添加谷歌网站和密钥作为设置。他们工作,因为验证码显示在前端的评论上。
在comments.php
我已添加
add_filter('comment_form','add_google_captcha');
function add_google_captcha(){
$google_site_recaptcha = get_option('recaptcha_site_key');
echo '<div class="g-recaptcha" data-sitekey="'.$google_site_recaptcha.'"></div>';
}
在我的functions.php
我已经收到了recaptcha api
wp_enqueue_script( 'google_recaptcha_js', 'https://www.google.com/recaptcha/api.js','',THEME_VERSION, false);
我已经尝试在页脚和标题中设置它,但仍然会发生同样的事情。
我还对我的google_recaptcha.php
进行了本地化,以便我可以在我的ajax调用中访问它
wp_localize_script( 'custom', 'ajax_posts', array(
'ajaxurl' => $ajaxurl,
'google_captcha' => get_template_directory_uri(). '/inc/google_recaptcha.php' ,
'google_captcha_check' => esc_html__('Please fill in the required fields and check the captcha', 'mytheme'),
));
我的点击活动如下:
/* Google */
$("#comment-submit").on('click', function(e){
console.log('bla');
var data_2;
$.ajax({
type: "POST",
url: 'ajax_posts.google_captcha',
data: $('#commentform').serialize(),
async: false,
success: function(data) {
console.log(data);
if(data.nocaptcha === "true"){
data_2=1;
} else if(data.spam === "true") {
data_2=1;
} else {
data_2=0;
}
console.log(ajax_posts.google_captcha);
},
error : function (jqXHR, textStatus, errorThrown) {
console.log(jqXHR + ' :: ' + textStatus + ' :: ' + errorThrown);
}
});
if( data_2 !== 0 ){
e.preventDefault();
if(data_2 == 1){
$('.form-submit').after('<p class="captcha_alert">'+ajax_posts.google_captcha_check+'</p>');
setTimeout(function() { $(".captcha_alert").fadeOut(); }, 4000);
} else {
$('.form-submit').after('<p class="captcha_alert">'+ajax_posts.google_captcha_check+'</p>');
setTimeout(function() { $(".captcha_alert").fadeOut(); }, 4000);
}
} else {
$("#commentform").submit();
}
});
当我不检查重新接收时,我得到了
Object {nocaptcha: "true"}
没关系,验证码没有检查。但是,如果我检查它,并填写评论(或者不是这样的话),我会得到
POST http://www.example.com/wp-content/themes/mytheme/inc/google_recaptcha.php 500(内部服务器错误)
指向我的ajax函数(当我在控制台中展开它时,有:send,m.extend.ajax,(匿名函数)&lt; - 这是我的ajax调用,m.event.dispatch,r。处理)
我检查了所有内容,但我不知道可能出现的问题。密钥有效,因为验证码有效。 ajax中的url指向http://www.example.com/wp-content/themes/mytheme/inc/google_recaptcha.php
,这没关系。
当我从网络上检查表格数据时,我得到了g-recaptcha-response
,然后我在链接中粘贴了密钥和IP地址,我得到了
{
"success": false,
"error-codes": [
"missing-input-response",
"invalid-input-secret"
]
}
感谢任何帮助。
修改
添加了:
error : function (jqXHR, textStatus, errorThrown) {
console.log(jqXHR + ' :: ' + textStatus + ' :: ' + errorThrown);
}
到我的ajax,但只得到:
[object Object] :: error :: Internal Server Error
在我的控制台中。
编辑2
现在,当我粘贴回复时,我会收到invalid-input-response
和invalid-input-secret
:\
答案 0 :(得分:0)
解决了!
我不知道为什么我没有早点这样做,因为我多次这样做过。我为ajax
创建了一个句柄<?php
add_action( 'wp_ajax_google_recaptcha', 'mytheme_ajax_google_recaptcha' );
add_action( 'wp_ajax_nopriv_google_recaptcha', 'mytheme_ajax_google_recaptcha' );
function mytheme_ajax_google_recaptcha() {
$data;
header('Content-Type: application/json');
error_reporting(E_ALL ^ E_NOTICE);
if(isset($_POST['g-recaptcha-response'])){
$captcha=$_POST['g-recaptcha-response'];
}
if(!$captcha){
$data=array('nocaptcha' => 'true');
echo json_encode($data);
exit;
}
$google_secret_recaptcha = get_option('recaptcha_secret_key');
$response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$google_secret_recaptcha."&response=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']);
if($response.success==false) {
$data=array('spam' => 'true');
echo json_encode($data);
} else {
$data=array('spam' => 'false');
echo json_encode($data);
}
}
然后在我刚刚添加的ajax中,而不是url
,action: 'google_recaptcha',
它现在有效:D