WordPress 禁止某些用户登录

本文目录[隐藏]1步骤1:创建一个插件2步骤2:在资料页面添加选项3步骤3:禁止用户4步骤4:解禁用户5步骤5:判断用户是否被禁止6步骤6:阻止已禁止的用户登录

在某些特殊情况下,比如某些用户损害了网站的利益,你可能就需要禁止他们登录网站。禁止后,他们登录时将显示某些提示信息:

disable-user-wpdaxue_com

要实现类似的效果,伸手党可以直接下载安装 Disable Users 或者 User Control ,折腾党可以参考下面的教程自己做一个插件。

在下面的教程中,我们将一步步讲解插件的实现步骤,其中涉及到一些wp钩子和用户管理栏目的一些操作。

步骤1:创建一个插件

在 wp-content/plugins 目录创建一个名为“ban-users”的文件夹,然后在里面新建一个名为 ban-users.php 的php文件,在文件头部添加下面的代码:

1
2
3
4
5
6
7
8
9
<?php
/*
Plugin Name: Ban Users
Plugin URI: http://www.remicorson.com
Description: Allows you to ban users
Author: Remi Corson
Version: 1.0
Author URI: http://www.remicorson.com
*/

<?php
/*
Plugin Name: Ban Users
Plugin URI: http://www.remicorson.com
Description: Allows you to ban users
Author: Remi Corson
Version: 1.0
Author URI: http://www.remicorson.com
*/

步骤2:在资料页面添加选项

我们需要在用户的个人资料页面添加一个复选框,用来设置是否禁止该用户登录。继续在上面的文件中添加下面的代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/**
 * Admin init
 *
 * @access      public
 * @since       1.0 
 * @return      void
*/
function rc_admin_init(){
 
	// Edit user profile
	add_action( 'edit_user_profile', 'rc_edit_user_profile' );
	add_action( 'edit_user_profile_update', 'rc_edit_user_profile_update' );
 
}
add_action('admin_init', 'rc_admin_init' );

/**
* Admin init
*
* @access public
* @since 1.0
* @return void
*/
function rc_admin_init(){
// Edit user profile
add_action( ‘edit_user_profile’, ‘rc_edit_user_profile’ );
add_action( ‘edit_user_profile_update’, ‘rc_edit_user_profile_update’ );
}
add_action(‘admin_init’, ‘rc_admin_init’ );

上面的代码是用来将下面的两个函数挂载到对应的动作钩子。接下来,我们创建一个函数在个人资料页面添加一个复选框:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/**
 * Adds custom checkbox to user edition page
 *
 * @access      public
 * @since       1.0 
 * @return      void
*/
function rc_edit_user_profile() {
	if ( !current_user_can( 'edit_users' ) ) {
		return;
	}
 
	global $user_id;
 
	// User cannot disable itself
	$current_user = wp_get_current_user();
	$current_user_id = $current_user->ID;
	if ( $current_user_id == $user_id ) {
		return;
	}
	?>
	<h3></h3>
	<table class="form-table">
	<tr>
		<th scope="row"></th>
		<td><label for="rc_ban"><input name="rc_ban" type="checkbox" id="rc_ban"  /> </label></td>
	</tr>
	</table>
	<?php
}

/**
* Adds custom checkbox to user edition page
*
* @access public
* @since 1.0
* @return void
*/
function rc_edit_user_profile() {
if ( !current_user_can( ‘edit_users’ ) ) {
return;
}
global $user_id;
// User cannot disable itself
$current_user = wp_get_current_user();
$current_user_id = $current_user->ID;
if ( $current_user_id == $user_id ) {
return;
}
?>
<h3></h3>
<table class="form-table">
<tr>
<th scope="row"></th>
<td><label for="rc_ban"><input name="rc_ban" type="checkbox" id="rc_ban" /> </label></td>
</tr>
</table>
<?php
}

然后再添加一个函数来将这个选项的值保存到数据库中:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/**
 * Save custom checkbox
 *
 * @access      public
 * @since       1.0 
 * @return      void
*/
function rc_edit_user_profile_update() {
 
	if ( !current_user_can( 'edit_users' ) ) {
		return;
	}
 
	global $user_id;
 
	// User cannot disable itself
	$current_user    = wp_get_current_user();
	$current_user_id = $current_user->ID;
	if ( $current_user_id == $user_id ) {
		return;
	}
 
	// Lock
	if( isset( $_POST['rc_ban'] ) && $_POST['rc_ban'] = 'on' ) {
		rc_ban_user( $user_id );
	} else { // Unlock
		rc_unban_user( $user_id );
	}
 
}

/**
* Save custom checkbox
*
* @access public
* @since 1.0
* @return void
*/
function rc_edit_user_profile_update() {
if ( !current_user_can( ‘edit_users’ ) ) {
return;
}
global $user_id;
// User cannot disable itself
$current_user = wp_get_current_user();
$current_user_id = $current_user->ID;
if ( $current_user_id == $user_id ) {
return;
}
// Lock
if( isset( $_POST[‘rc_ban’] ) && $_POST[‘rc_ban’] = ‘on’ ) {
rc_ban_user( $user_id );
} else { // Unlock
rc_unban_user( $user_id );
}
}

正如你上面看到的一样,添加了两个函数 rc_ban_user() 和 rc_unban_user() ,前者是用来禁止用户,后者是解禁用户。下面我们将来完善这两个函数。

步骤3:禁止用户

现在我们可以创建一个 rc_ban_user() 来禁止用户,当然了,首先我们需要判断一下,是否该选项的值已经保存,如果没有,我们就需要保存这个值。所以我们下面还需要添加一个 rc_is_user_banned() 函数判断用户是否被禁止登录。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/**
 * Ban user
 *
 * @access      public
 * @since       1.0 
 * @return      void
*/
function rc_ban_user( $user_id ) {
 
	$old_status = rc_is_user_banned( $user_id );
 
	// Update status
	if ( !$old_status ) {
		update_user_option( $user_id, 'rc_banned', true, false );
	}
}

/**
* Ban user
*
* @access public
* @since 1.0
* @return void
*/
function rc_ban_user( $user_id ) {
$old_status = rc_is_user_banned( $user_id );
// Update status
if ( !$old_status ) {
update_user_option( $user_id, ‘rc_banned’, true, false );
}
}

步骤4:解禁用户

下面的函数和上面的更好相反,是用来解禁用户的:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/**
 * Un-ban user
 *
 * @access      public
 * @since       1.0 
 * @return      void
*/
function rc_unban_user( $user_id ) {
 
	$old_status = rc_is_user_banned( $user_id );
 
	// Update status
	if ( $old_status ) {
		update_user_option( $user_id, 'rc_banned', false, false );
	}
}

/**
* Un-ban user
*
* @access public
* @since 1.0
* @return void
*/
function rc_unban_user( $user_id ) { $old_status = rc_is_user_banned( $user_id );
// Update status
if ( $old_status ) {
update_user_option( $user_id, ‘rc_banned’, false, false );
}
}

步骤5:判断用户是否被禁止

在上面的 rc_ban_users() 和 rc_unban_users() 这两个函数中,我们都调用了一个叫 rc_is_user_banned() 的函数用来判断用户是否被禁止,所以我们下面就要完善这个函数:

1
2
3
4
5
6
7
8
9
10
/**
 * Checks if a user is already banned
 *
 * @access      public
 * @since       1.0 
 * @return      void
*/
function rc_is_user_banned( $user_id ) {
	return get_user_option( 'rc_banned', $user_id, false );
}

/**
* Checks if a user is already banned
*
* @access public
* @since 1.0
* @return void
*/
function rc_is_user_banned( $user_id ) {
return get_user_option( ‘rc_banned’, $user_id, false );
}

基本上,这个函数只是返回保存在 rc_ban_users() 上的值。

现在我们已经可以在编辑用户的页面看到添加的设置选项了:

ban-user-wpdaxue_com

我们还需要最后一步,限制被禁止的用户登录网站。

步骤6:阻止已禁止的用户登录

要实现这个功能,我们需要用到 wp_authenticate_user 钩子,现在我们创建一个 rc_authenticate_user() 函数:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/**
 * Check if user is locked while login process
 *
 * @access      public
 * @since       1.0 
 * @return      void
*/
function rc_authenticate_user( $user ) {
 
	if ( is_wp_error( $user ) ) {
		return $user;
	}
 
	// Return error if user account is banned
	$banned = get_user_option( 'rc_banned', $user->ID, false );
	if ( $banned ) {
		return new WP_Error( 'rc_banned', __('<strong>ERROR</strong>: This user account is disabled.', 'rc') );
	}
 
	return $user;
}

/**
* Check if user is locked while login process
*
* @access public
* @since 1.0
* @return void
*/
function rc_authenticate_user( $user ) { if ( is_wp_error( $user ) ) {
return $user;
}
// Return error if user account is banned
$banned = get_user_option( ‘rc_banned’, $user->ID, false );
if ( $banned ) {
return new WP_Error( ‘rc_banned’, __(‘<strong>ERROR</strong>: This user account is disabled.’, ‘rc’) );
}
return $user;
}

然后将该函数挂载到 wp_authenticate_user 钩子:

1
add_filter( 'wp_authenticate_user', 'rc_authenticate_user', 1 );

add_filter( ‘wp_authenticate_user’, ‘rc_authenticate_user’, 1 );

到这里,整个插件就完成了。

你可以在编辑用户的界面,设置是否禁止用户,如果该用户被禁止了,他就无法登录,同时显示提示信息。ERROR : This user account is disabled.

参考资料:http://www.wpexplorer.com/how-to-ban-a-wordpress-user/

本人擅长Ai、Fw、Fl、Br、Ae、Pr、Id、Ps等软件的安装与卸载,精通CSS、JavaScript、PHP、ASP、C、C++、C#、Java、Ruby、Perl、Lisp、Python、Objective-C、ActionScript、Pascal等单词的拼写,熟悉Windows、Linux、OS X、Android、iOS、WP8等系统的开关机。

通过下面的方式来联系我们:

电邮:138762189@qq.com

联系QQ:点击这里给我发消息

官方站:www.tadke.com

※ ※ 联系请加我的企鹅号 ※※

※ ※技术支持请微信联系站长 ※※

Copyright © 2023 Tadke.com. 琼ICP备20000547号