Get the Official WPSaviour App Now!

BlogTutorials

How To Ban A WordPress User

I was requested quite a few days up to now to create a system would ban a WordPress individual from an arrange. So I created a simple plugin and I wanted to share with you the basics of this plugin. On this tutorial we is likely to be masking WordPress filters, actions, clients columns administration and some further good points.

Step 1: Create The Plugin

As quickly as as soon as extra, proper right here it’s very tough, all you need to do is to create a model new folder with “wp-content/plugins” referred to as “ban-users”. On this folder, create a model new file referred to as “ban-users.php”, open it an paste this code:

<?php
/*
Plugin Establish: Ban Clients
Plugin URI: http://www.remicorson.com
Description: Lets you ban clients
Creator: Remi Corson
Mannequin: 1.0
Creator URI: http://www.remicorson.com
*/

Step 2: Add A Checkbox On Clients’ Profile Internet web page

The very very first thing we’ve to to do is in order so as to add a checkbox on each individual profile model net web page. Everytime you’ll confirm this checkbox it will retailer an individual meta risk that may indicated that the individual isn’t allowed anymore to login to your site.

Proper right here is the code:

/**
 * Admin init
 *
 * @entry      public
 * @since       1.0 
 * @return      void
*/
carry out rc_admin_init(){
    
	// Edit individual 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' );

This code is solely making a reputation to a carry out that we now should create now. This carry out will add a checkbox to clients profile net web page.

/**
 * Supplies custom-made checkbox to individual model net web page
 *
 * @entry      public
 * @since       1.0 
 * @return      void
*/
carry out rc_edit_user_profile() {
	if ( !current_user_can( 'edit_users' ) ) {
		return;
	}
	
	world $user_id;
	
	// Shopper cannot disable itself
	$current_user = wp_get_current_user();
	$current_user_id = $current_user->ID;
	if ( $current_user_id == $user_id ) {
		return;
	}

	// Check if enabled
	$checked = checked( get_user_option( 'rc_banned', $user_id, false ) );

	// Present checkbox
	echo '<desk class="form-table">
	<tr>
		<th scope="row">Ban Shopper</th>
		<td>
		<label for="rc_ban">
			<enter establish="rc_ban" form="checkbox" id="rc_ban" '. $checked .' />Ban this individual</label>
		</td>
	</tr>
	</desk>';
}

We now should have the carry out that may save into the database the price of the checkbox:

/**
 * Save custom-made checkbox
 *
 * @entry      public
 * @since       1.0 
 * @return      void
*/
carry out rc_edit_user_profile_update() {
			
	if ( !current_user_can( 'edit_users' ) ) {
		return;
	}
	
	world $user_id;
	
	// Shopper 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 );
	}
	
}

As you’ll see this new carry out makes use of two completely different options: rc_ban_users() and rc_unban_users(). Their names are pretty categorical, the first one will retailer throughout the database the reality that an individual is ban, the second will unlock clients.

Step 3: Ban Clients

It’s now time to create the rc_ban_users() carry out. On this carry out we’ve to confirm if a value is already saved, and if not we’ve to retailer the price. That’s the reason why i identify a carry out that I’ll describe later: rc_is_user_banned():

/**
 * Ban individual
 *
 * @entry      public
 * @since       1.0 
 * @return      void
*/
carry out rc_ban_user( $user_id ) {
	
	$old_status = rc_is_user_banned( $user_id );
	
	// Change standing
	if ( !$old_status ) {
		update_user_option( $user_id, 'rc_banned', true, false );
	}
}

Step 4: Un-ban Clients

The following carry out is the choice as a result of the one we merely created: we now have to offer the flexibleness to “un-ban” clients:

/**
 * Un-ban individual
 *
 * @entry      public
 * @since       1.0 
 * @return      void
*/
carry out rc_unban_user( $user_id ) {

	$old_status = rc_is_user_banned( $user_id );
	
	// Change standing
	if ( $old_status ) {
		update_user_option( $user_id, 'rc_banned', false, false );
	}
}

Step 5: Is Shopper Banned?

We seen in rc_ban_users() and rc_unban_users() that we use a carry out referred to as rc_is_user_banned() to confirm if an individual is banned or not. Let’s create it:

/**
 * Checks if an individual is already banned
 *
 * @entry      public
 * @since       1.0 
 * @return      void
*/
carry out rc_is_user_banned( $user_id ) {
	return get_user_option( 'rc_banned', $user_id, false );
}

Principally, this carry out merely returns the price of the selection saved in rc_ban_users().

In the intervening time we now have a model new checkbox on the individual model net web page that ought to love that:

ban_users_1

The ultimate step is to hook a carry out to the login sort to stay away from banned clients to login.

Step 5: Stay away from Banned Clients To Login

To take motion we’ve to make use of a WordPress default filter referred to as “wp_authenticate_user”. To this  filter we’ll hook a carry out referred to as “rc_authenticate_user()”. This carry out will use the WP_Error class.

/**
 * Check if individual is locked whereas login course of
 *
 * @entry      public
 * @since       1.0 
 * @return      void
*/
carry out rc_authenticate_user( $individual ) {

	if ( is_wp_error( $individual ) ) {
		return $individual;
	}
	
	// Return error if individual account is banned
	$banned = get_user_option( 'rc_banned', $individual->ID, false );
	if ( $banned ) {
		return new WP_Error( 'rc_banned', __('<strong>ERROR</strong>: This individual account is disabled.', 'rc') );
	}
	
	return $individual;
}

Now, we merely need in order so as to add the filter:

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

Conclusion

We created a plugin that gives a checkbox to the individual profile model net web page.  Used a second carry out to retailer the price on the checkbox and we create a carry out to ban a WordPress individual and one different on to unlock an individual. Moreover created a small carry out to confirm if an individual is banned or not. And we lastly hooked a carry out to the “wp_authenticate_user” filter using the WP_Error WordPress default class.

Download The WPSaviour App Now

Related posts
Beginner’s GuideBlogTutorials

Use Gutenberg like a pro with keyboard shortcuts

Beginner’s GuideBlogSecurityWordPress Security

A Simple Explanation of SSL Certificate Errors & How to Fix Them

Beginner’s GuideBest WordPress PluginsBlog

9 Best A/B Split Testing Plugins for WordPress

Best WordPress PluginsBlog

7 Best Plugins for Tracking User Activity in WordPress

Sign up for our Newsletter and
stay informed

Fear Of Missing Out?

Sign up with your email address to receive WordPress tips and updates
SUBSCRIBE NOW
Terms and Conditions apply
close-link
Click Me