如何设置让WordPress用户每篇文章评论一次
发布网友
发布时间:2022-04-21 05:58
我来回答
共1个回答
热心网友
时间:2022-06-18 08:57
实现代码,放到当前主题的functions.php中即可(这里还增加了对IP的判断,更保险):
// 获取评论用户的ip,参考wp-includes/comment.php
function ludou_getIP() {
$ip = $_SERVER['REMOTE_ADDR'];
$ip = preg_replace( '/[^0-9a-fA-F:., ]/', '', $ip );
return $ip;
}
function ludou_only_one_comment( $commentdata ) {
global $wpdb;
$currentUser = wp_get_current_user();
// 不*管理员发表评论
if(empty($currentUser->roles) || !in_array('administrator', $currentUser->roles)) {
$bool = $wpdb->get_var("SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = ".$commentdata['comment_post_ID']." AND (comment_author = '".$commentdata['comment_author']."' OR comment_author_email = '".$commentdata['comment_author_email']."' OR comment_author_IP = '".ludou_getIP()."') LIMIT 0, 1;");
if($bool)
wp_die('本站每篇文章只允许评论一次。<a href="'.get_permalink($commentdata['comment_post_ID']).'">点此返回</a>');
}
return $commentdata;
}
add_action( 'preprocess_comment' , 'ludou_only_one_comment', 2