Php Wordpress显示/隐藏评论表单
我的wordpress主题有问题。 我无法创建显示/隐藏评论表单的按钮 我有一个comments.php文件,其中包含以下代码:Php Wordpress显示/隐藏评论表单,php,jquery,wordpress,Php,Jquery,Wordpress,我的wordpress主题有问题。 我无法创建显示/隐藏评论表单的按钮 我有一个comments.php文件,其中包含以下代码: if ( ! comments_open() && '0' != get_comments_number() && post_type_supports( get_post_type(), 'comments' ) ) : ?> <p class="no-comments">
if ( ! comments_open() && '0' != get_comments_number() && post_type_supports( get_post_type(), 'comments' ) ) :
?>
<p class="no-comments"><?php _e( 'Comments are close.', 'ffita' ); ?></p>
<?php endif; ?>
<?php comment_form(); ?>
comment_form();
然后用这种方式修改comment.php
if ( ! comments_open() && '0' != get_comments_number() && post_type_supports( get_post_type(), 'comments' ) ) :
?>
<p class="no-comments"><?php _e( 'Comments are close.', 'ffita' ); ?></p>
<?php endif; ?>
<div class="commenti">
<button type="button" id="ffita-add-comment" class="btn btn-primary">Add Comment</button>
<script>
$(document).ready(function(){
$("#ffita-add-comment").click(function(){
event.preventDefault();
$.ajax({
type: 'GET',
url: '/wordpress/wp-content/themes/ffita/template-parts/comment_form.php',
success: function(response){
$( '#ffita-comment-form' ).html( response );
},
});
});
});
</script>
<div id="ffita-comment-form">
</div>
<?php //comment_form(); ?>
但点击按钮会导致:
致命错误:调用未定义的函数注释表单
所以我的问题是:如何创建一个按钮,只在点击时加载wordpress评论表单功能?
我在我的博客上几乎没有评论,如果没有必要,我不想加载不必要的代码,比如表单和验证码
谢谢在我之前的问题中,我有两个问题: 在wordpress帖子中显示/隐藏评论表单 只有当用户想要发表评论时才需要运行google recaptcha插件 从逻辑角度来看,所采用的解决方案如下: 禁用以前使用的插件 默认:通过css隐藏wp注释表单部分 添加按钮添加评论。。。单击时,将显示禁用“提交”按钮的注释表单 当用户选择输入注释的文本区域->谷歌验证码 验证验证码,如果确定->启用wp注释表单中的提交按钮 经过各种研究和测试,我以这种方式解决了。。。。感谢stackoverflow上的各种帖子 我卸载了用于插入google recaptcha的插件 在function.php中,当我将js脚本排入队列时,我添加了: 在style.css文件中,我插入了以下代码: 在我的模板中,我以以下方式更改了comments.php: 注意:在ffita表格激活功能中,我有两种不同的方法来测试验证码响应。。。基于simple_chk var。 如果simple_chk为true,我将检查从grecaptcha.render收到的响应的长度。。。。如果响应长度不是0,我将评估验证码是否已解决 如果simple_chk为false,我将响应值传递给php文件g_recaptcha_verify.php进行验证 我创建g_recaptcha_verify.php用于验证码的服务器端验证 注意:我在这个示例脚本中使用了google的开发者密钥。 在生产环境中使用自己的密钥 完成了!!您可以在以下网站的任何帖子上看到一个工作示例:
wp_enqueue_script( 'ffita_base_functions_js', get_template_directory_uri() . '/inc/js/ffita_base_functions.min.js', array ( 'jquery' )); //enqueue js script
wp_localize_script('ffita_base_functions_js', 'ffita_var', array("ffita_theme_path"=> get_template_directory_uri()) ); //use wp_localize_sript to pass var data from PHP to JavaScript
.comment-respond{
visibility: hidden; //default wordpress class for comment form
}
#ffita-captcha{
margin: 30px;
}
<button type="button" class="btn btn-primary ffita-add-comment">Add a comment</button>
<div id="ffita-captcha"> </div> //div where google captcha will be displayed
<?php
$comments_args = array(
'class_submit' => 'submit ffita-captcha-submit', //add a class to standar submit button
);
comment_form($comments_args); ?> //wordpress function to call comments form
<script>
$(document).ready(function() { //jquery function
var id_activation='comment'; //div id where click active captcha
var id_captcha_view='ffita-captcha'; //div id where captcha will be displayed
$('.ffita-captcha-submit').prop('disabled',true); //disable comment form submit button
$('#'+id_activation).on('click', function(){ //when click on comment text area
ffita_show_captcha(id_captcha_view); //call js function and pass id where display captcha
});
$('.ffita-add-comment').on('click', function(){ //jquery for button...., when clicked
$(this).hide(); //hide button
$('.comment-respond').attr("style", "visibility: visible"); //show comment form
});
});
</script>
function ffita_form_activate(resp){ //function call from the next
var simple_chk=false; //false or true. see below notes
if (simple_chk){
if (resp.length != 0 ) { //if response lenght is not 0 I evaluate captcha solved
$('.ffita-captcha-submit').prop('disabled',false); //enable wp comment submit button
}
else {
alert ("try again");
grecaptcha.reset(); //on failed reset captcha
}
}
else {
if (resp.length != 0 ) {
local_path=ffita_var.ffita_theme_path; //var passed through wp_localize_script (theme root dir)
$.ajax({
url: local_path+'/inc/g_recaptcha_verify.php', //php file for captcha verification see below
type:'POST',
data:{resp:resp},
success: function (response) { //php file returns OK if captcha was solved correctly
if (response=="OK"){
$('.ffita-captcha-submit').prop('disabled',false);
}
else {
alert ("try again")
grecaptcha.reset();
}
}
});
}
else {
alert ("try again");
grecaptcha.reset();
}
}
}
var captchaLoaded = false;
function ffita_show_captcha(id_captcha_view){ //function call from jquery - comment.php
// If we have loaded script once already, exit.
if (captchaLoaded) {
return;
}
// Variable Intialization
var head = document.getElementsByTagName('head')[0];
var recaptchaScript = document.createElement('script');
//key for develop (site key)
var recaptchaKey = '6LeIxAcTAAAAAJcZVRqyHh71UMIEGNQ_MXjiZKhI'; //is key for developer...insert you public key!!
// Dynamically add Recaptcha Script
recaptchaScript.type = 'text/javascript';
recaptchaScript.src = 'https://www.google.com/recaptcha/api.js';
// Dynamically load script
head.appendChild(recaptchaScript);
//check grecaptcha is defined otherwise I wait
var poll;
var timeout = 200;
poll = function () {
setTimeout(function () {
timeout--;
if (typeof grecaptcha !== 'undefined') {
grecaptcha.render(id_captcha_view, //where catpcha is shown
{
'sitekey': recaptchaKey,
'theme': "light",
'callback': ffita_form_activate, //call for prev function, return response from google function
}
);
}
else if (timeout > 0) {
poll();
}
else {
alert ("Failed to load. Please retry");
}
}, 200);
};
poll();
//Set flag to only load once
captchaLoaded = true;
}
<?php
//developer value
//Site key: 6LeIxAcTAAAAAJcZVRqyHh71UMIEGNQ_MXjiZKhI
//Secret key: 6LeIxAcTAAAAAGG-vFI1TnRWxMZNFuojJ4WifJWe
// check post value:
if (isset($_POST['resp'])) {
// Create POST request:
$recaptcha_url = 'https://www.google.com/recaptcha/api/siteverify';
$recaptcha_secret = '6LeIxAcTAAAAAGG-vFI1TnRWxMZNFuojJ4WifJWe'; //insert your secret key!!
$recaptcha_response = $_POST['resp'];
// Send POST request and decode respone:
$recaptcha = file_get_contents($recaptcha_url . '?secret=' . $recaptcha_secret . '&response=' . $recaptcha_response);
$recaptcha = json_decode($recaptcha);
// Google response evaluation:
if ($recaptcha && $recaptcha->success) {
echo ("OK"); //used in jquery
} else {
//
echo ("NO_OK"); //use in jquery
}
}
?>