- This topic has 0 replies, 1 voice, and was last updated 5 months, 3 weeks ago by .
Viewing 0 reply threads
Viewing 0 reply threads
- You must be logged in to reply to this topic.
Welcome › mkmcst community Forum › Web programming › WordPress › Random wordpress › How to Ban Users Accounts in WordPress
Are you looking for a way to allow the admin to band WordPress user accounts? While there’s probably a plugin for this, we have created a quick code snippet that you can use to ban users accounts in WordPress.
// display checkbox to admin
add_action( 'edit_user_profile', 'ban_user_profile_fields' );
function ban_user_profile_fields( $user ) {
global $current_user;
if ( current_user_can( 'edit_user' ) && $user->ID != $current_user->ID ){
$status = get_the_author_meta( 'ban_user', $user->ID );
?>
<h3><?php _e("Account Status", "blank"); ?></h3>
<table class="form-table">
<tr>
<th>Ban User</th>
<td><label for="ban_user"><input type="checkbox" name="ban_user" id="ban_user" value="1" <?php if($status == 1){ echo ' checked'; } ?> /></label>
<span class="description"><?php _e("Check this option to ban this users account."); ?></span>
</td>
</tr>
</table>
<?php
}
}
// Save profile update
add_action( 'edit_user_profile_update', 'save_extra_user_profile_fields' );
function save_extra_user_profile_fields( $user_id ){
if ( !current_user_can( 'edit_user', $user_id ) ) { return false; }
update_usermeta( $user_id, 'ban_user', $_POST['ban_user'] );
}
// Check if user is banned
add_filter( 'wp_authenticate_user', 'login_ban_status', 1 );
function login_ban_status($user) {
if ( is_wp_error( $user ) ) { return $user; }
$status = get_user_meta( $user->ID, 'ban_user', 'true' );
if($status == 1){
return new WP_Error( 'banned', __('<strong>ERROR</strong>: This user account has been banned.', 'banned') );
}
return $user;
}