WordPress教程

WordPress 纯代码添禁止某些用户登录

阿里云


在某些特殊情况下某些用户损害了网站的利益,你可能就需要禁止他们登录网站,WordPress 博客如何实现免插件纯代码添加禁止某些用户登录的方法,也就是 WordPress 博客用户怎么实现封号,近日以来网站垃圾信息,垃圾回复泛滥,由于之前不怎么关注这块,但是现在不得不进行梳理一下拿出一个解决方案了,做网站的朋友都知道,自己辛辛苦苦写的文章,发的资源为了防止和谐只提供给网站注册会员使用,需要留言回复文章才可以获取下载地址,但是有一些伸手党随意的乱回复,乱灌水,简直就是一点对起码尊重都没有,好吧为了此问题这下研究了下。

WordPress 博客属于一个比较成熟的开源程序,安全性还是比较高的,官方也一直在更新维护,而且还是免费,WordPress 博客的 DIY 可塑性很牛,由于 WordPress 博客默认很多功能是没有的,比如这个“WP 禁止某些用户登录”都是需要自己 DIY 加进去的,当然了如果你觉得麻烦伸手党可以直接下载安装 Disable Users 或者 User Control 这 2 个插件的任意一个来实现。下面为大家谈一谈 WordPress 博客如何实现纯代码添加禁止某些用户登录的方法,由于插件太多会导致网站的效率降低,网站打开速度越来越卡,或者越来越慢,我们不需要利用插件只需要一串代码即可实现。我们只需要在在当前使用的主题目录下的“ functions.php”中加入以下代码即可:

也想出现在这里?联系我们
创客主机
  1. /**
  2.  * WordPress 禁止某些用户登录
  3.  */
  4. //在资料页面添加选项
  5. function lxtx_rc_admin_init(){ 
  6. 	// 编辑用户资料
  7. 	add_action( 'edit_user_profile', 'lxtx_rc_edit_user_profile' );
  8. 	add_action( 'edit_user_profile_update', 'lxtx_rc_edit_user_profile_update' ); 
  9. }
  10. add_action('admin_init', 'lxtx_rc_admin_init' );
  11.  
  12. //在个人资料页面添加一个复选框
  13. function lxtx_rc_edit_user_profile() {
  14. 	if ( !current_user_can( 'edit_users' ) ) {
  15. 		return;
  16. 	} 
  17. 	global $user_id; 
  18. 	// 用户不能禁止自己
  19. 	$current_user = wp_get_current_user();
  20. 	$current_user_id = $current_user->ID;
  21. 	if ( $current_user_id == $user_id ) {
  22. 		return;
  23. 	}
  24. 	?>
  25. 	<h3>权限设置</h3>
  26. 	<table class="form-table">
  27. 	<tr>
  28. 		<th scope="row">禁止用户登录</th>
  29. 		<td><label for="lxtx_rc_ban"><input name="lxtx_rc_ban" type="checkbox" id="lxtx_rc_ban" 
  30. 		<?php if (lxtx_rc_is_user_banned( $user_id )){echo 'checked="checked"';} ?> /> 请谨慎操作,选中则禁止!</label></td>
  31. 	</tr>
  32. 	</table>
  33. 	<?php
  34. }
  35.  
  36. //添加一个函数来将这个选项的值保存到数据库中
  37. function lxtx_rc_edit_user_profile_update() { 
  38. 	if ( !current_user_can( 'edit_users' ) ) {
  39. 		return;
  40. 	} 
  41. 	global $user_id; 
  42. 	// 用户不能禁止自己
  43. 	$current_user    = wp_get_current_user();
  44. 	$current_user_id = $current_user->ID;
  45. 	if ( $current_user_id == $user_id ) {
  46. 		return;
  47. 	} 
  48. 	// 锁定
  49. 	if( isset( $_POST['lxtx_rc_ban'] ) && $_POST['lxtx_rc_ban'] = 'on' ) {
  50. 		lxtx_rc_ban_user( $user_id );
  51. 	} else { // 解锁
  52. 		lxtx_rc_unban_user( $user_id );
  53. 	} 
  54. }
  55.  
  56. //禁止用户
  57. function lxtx_rc_ban_user( $user_id ) { 
  58. 	$old_status = lxtx_rc_is_user_banned( $user_id ); 
  59. 	// 更新状态
  60. 	if ( !$old_status ) {
  61. 		update_user_option( $user_id, 'lxtx_rc_banned', true, false );
  62. 	}
  63. }
  64.  
  65. //解禁用户
  66. function lxtx_rc_unban_user( $user_id ) { 
  67. 	$old_status = lxtx_rc_is_user_banned( $user_id ); 
  68. 	// 更新状态
  69. 	if ( $old_status ) {
  70. 		update_user_option( $user_id, 'lxtx_rc_banned', false, false );
  71. 	}
  72. }
  73.  
  74. //判断用户是否被禁止
  75. function lxtx_rc_is_user_banned( $user_id ) {
  76. 	return get_user_option( 'lxtx_rc_banned', $user_id, false );
  77. }
  78.  
  79. //阻止已禁止的用户登录
  80. function lxtx_rc_authenticate_user( $user ) { 
  81. 	if ( is_wp_error( $user ) ) {
  82. 		return $user;
  83. 	} 
  84. 	// 如果用户被禁止,则返回错误提示
  85. 	$banned = get_user_option( 'lxtx_rc_banned', $user->ID, false );
  86. 	if ( $banned ) {
  87. 		return new WP_Error( 'lxtx_rc_banned', __('抱歉,该用户被禁止登录!请联系站长解禁。', 'rc') );
  88. 	} 
  89. 	return $user;
  90. }
  91. //将该函数挂载到 wp_authenticate_user 钩子
  92. add_filter( 'wp_authenticate_user', 'lxtx_rc_authenticate_user', 1 );

在我们的当前主题添加上面的代码后,我们即可在后台“编辑用户”的菜单里看到我们增加的“禁止用户登录”选项了;选中后,则会禁止该用户登录,如下图提示:

以上就是 WP 博客如何实现纯代码添加禁止某些用户登录的方法的全部教程,如果有更好的方法欢迎留言提出来,大家一起探讨交流。

WordPress 纯代码添禁止某些用户登录

已有 415 人购买
查看演示升级 VIP立刻购买

收藏
(0)

发表回复

热销模板

Ashade - 作品展示摄影相册WordPress汉化主题
LensNews

本站承接 WordPress / PbootCMS / DedeCMS 等
系统建站、仿站、开发、定制等业务!