【wordpress主题】WordPress中实现每篇文章只允许用户评论一次

更新时间:2020-02-12    来源:WordPress    手机版     字体:

【www.bbyears.com--WordPress】

之前有网友提出,在WordPress中有没有办法实现每篇文章只允许用户评论一次?

暂不说这个需求有没有用,毕竟WordPress就是给有各种需求的人用的。这个功能实现起来也比较简单,只需每次用户发表的评论进数据库之前,从当前文章的所有评论中查找是否有相同的用户名或邮箱已经发表过评论,如果有就跳到错误页面即可。

实现代码,放到当前主题的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("本站每篇文章只允许评论一次。点此返回");
  }
 
  return $commentdata;
}
add_action( "preprocess_comment" , "ludou_only_one_comment", 20);

本文来源:http://www.bbyears.com/wangyezhizuo/85165.html