纯代码禁止WordPress某些用户评论

使用 wordpress 已经好几年了,但是我们发现带有会员注册的 wordpress 站点居然都没有禁言功能,如果我们网站在某些时候,个别会员用户的评论严重违反站点规定,那么我们就需对该会员用户进行禁言处理了以示惩罚。于是想折腾折腾看看有什么办法,于是上网搜索相关教程但是基本都是关于 WordPress 文章禁止评论的… 所以只能自己动手测试操作了,具体代码如下:

  1. /**
  2.  * WordPress 禁止某些用户评论
  3.  */
  4. //在资料页面添加选项
  5. function lxtx_usercmt_admin_init(){ 
  6. // 编辑用户资料
  7. add_action( 'edit_user_profile', 'lxtx_usercmt_edit_user_profile' );
  8. add_action( 'edit_user_profile_update', 'lxtx_usercmt_user_profile_update' ); 
  9. }
  10. add_action('admin_init', 'lxtx_usercmt_admin_init' );
  11. //在个人资料页面添加一个复选框
  12. function lxtx_usercmt_edit_user_profile() {
  13. if ( !current_user_can( 'edit_users' ) ) {
  14. return;
  15. } 
  16. global $user_id; 
  17. // 用户不能禁止自己
  18. $current_user = wp_get_current_user();
  19. $current_user_id = $current_user->ID;
  20. if ( $current_user_id == $user_id ) {
  21. return;
  22. }
  23. ?>
  24. <!-- <h3>权限设置</h3> -->
  25. <table class="form-table">
  26. <tr>
  27. <th scope="row">禁止用户评论</th>
  28. <td><label for="lxtx_usercmt_ban"><input name="lxtx_usercmt_ban" type="checkbox" id="lxtx_usercmt_ban" 
  29. <?php if (lxtx_usercmt_is_user_banned( $user_id )){echo 'checked="checked"';} ?> /> 请谨慎操作,选中则禁止!</label></td>
  30. </tr>
  31. </table>
  32. <?php
  33. }
  34. //添加一个函数来将这个选项的值保存到数据库中
  35. function lxtx_usercmt_user_profile_update() { 
  36. if ( !current_user_can( 'edit_users' ) ) {
  37. return;
  38. } 
  39. global $user_id; 
  40. // 用户不能禁止自己
  41. $current_user    = wp_get_current_user();
  42. $current_user_id = $current_user->ID;
  43. if ( $current_user_id == $user_id ) {
  44. return;
  45. } 
  46. // 锁定
  47. if( isset( $_POST['lxtx_usercmt_ban'] ) && $_POST['lxtx_usercmt_ban'] = 'on' ) {
  48. lxtx_usercmt_ban_user( $user_id );
  49. } else { // 解锁
  50. lxtx_usercmt_unban_user( $user_id );
  51. } 
  52. }
  53. //禁止用户
  54. function lxtx_usercmt_ban_user( $user_id ) { 
  55. $old_status = lxtx_usercmt_is_user_banned( $user_id ); 
  56. // 更新状态
  57. if ( !$old_status ) {
  58. update_user_option( $user_id, 'lxtx_usercmt_banned', true, false );
  59. }
  60. }
  61. //解禁用户
  62. function lxtx_usercmt_unban_user( $user_id ) { 
  63. $old_status = lxtx_usercmt_is_user_banned( $user_id ); 
  64. // 更新状态
  65. if ( $old_status ) {
  66. update_user_option( $user_id, 'lxtx_usercmt_banned', false, false );
  67. }
  68. }
  69. //判断用户是否被禁止
  70. function lxtx_usercmt_is_user_banned( $user_id ) {
  71. return get_user_option( 'lxtx_usercmt_banned', $user_id, false );
  72. }

把上面代码加入 functions.php 后,后台“编辑用户”时,您就会发现多了一个“禁止用户评论”的选项如下:

我们发现上面只是实现了禁言的方法,下面就来把禁言的功能真正使用起来(登录回复可见短代码一直没用过,这次就来用上试试效果~ )。

  1. //阻止已禁止的用户评论
  2. function lxtx_usercmt_authenticate_user( $commentdata ) {
  3.     if( lxtx_usercmt_is_user_banned( $commentdata['user_ID'] ) ){
  4.         err(__('由于严重违反龙笑天下相关规定,该账号已被禁言'));
  5.     }else{
  6.         return $commentdata;
  7.     }
  8. }
  9. if( is_user_logged_in() ){
  10.     add_filter( 'preprocess_comment' , 'lxtx_usercmt_authenticate_user' );
  11. }

具体的禁言效果如下图:

是不是简单实用,如果感觉不错您就来照葫芦画瓢做一个吧!

© 版权声明
THE END
喜欢就支持以下吧
点赞0赞赏
分享
相关推荐
  • 暂无相关文章
评论 抢沙发