之前有网友提出,在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('本站每篇文章只容许评论一次。<a href="'.get_permalink($commentdata['comment_post_ID']).'">点此前往</a>'); } return $commentdata; } add_action( 'preprocess_comment' , 'ludou_only_one_comment', 20);
这里没无限制治理员的评论次数,那咱们顺带着看一下判别用户能否为治理员的办法:
判别指定id的用户是不是治理员
该需要完成起来十分简略,几行代码搞定,分享一下:
function ludou_is_administrator($user_id) { $user = get_userdata($user_id); if(!empty($user->roles) && in_array('administrator', $user->roles)) return 1; // 是治理员 else return 0; // 非治理员 }
判别以后登录用户是不是治理员
假如是判别以后登录用户是不是治理员,能够应用上面的函数:
function ludou_is_administrator() { // wp_get_current_user函数仅限在主题的functions.php中应用 $currentUser = wp_get_current_user(); if(!empty($currentUser->roles) && in_array('administrator', $currentUser->roles)) return 1; // 是治理员 else return 0; // 非治理员 }
以上就是安达网络工作室关于《WordPress中限制非管理员用户在文章后只能评论一次》的一些看法。更多内容请查看本栏目更多内容!
要害字形容:罕用 插件 代码 以及 &mdash 文章 即可   而后 统计 这篇文章是我之前为一个香港人...
代码如下:<?php/*在根目录 -> wp-content -> themes 下创立mytheme文件夹用来寄存创立新主题模板 在mythem...
post_class() post_class 是 WordPress 内置的一个用于显示文章 class 称号的函数,该函数通常会为每一篇文...
PHP 自身是无奈创立定时义务的,然而 WordPress 自带了一个伪定时义务(Cron) API,十分的不便好用,包括 ...
小洞不补大洞享乐。关于bloggers来说这是永恒的真谛,仅仅花一点工夫在马上就晋级上省下了很多之后修复一些...
WordPress是支流的 Blog 搭建平台。 WordPress 能够说是世界上目前最先进的 weblog 顺序。目前开发的顺...