Can you generate a small wordpress plugin that will allow me to ban users for a…
- User
Can you generate a small wordpress plugin that will allow me to ban users for a specified amount of time
- Assistant
Can you generate a small wordpress plugin that will allow me to ban users for a specified amount of time
Here’s a small WordPress plugin that allows you to ban users for a specified amount of time. The plugin includes an admin interface where you can ban users by their email or username, and it automatically lifts the ban after the specified time.
Plugin Code:temporary-ban-users.php
────────────────────────────────────────────────────────────────────
phpCopy code<?php /* Plugin Name: Temporary User Ban Description: Temporarily ban users for a specified amount of time. Version: 1.0 Author: Your Name */
if (!defined('ABSPATH')) exit;
class TemporaryUserBan {
public function __construct() {
add_action('admin_menu', [$this, 'add_admin_page']);
add_filter('authenticate', [$this, 'check_ban_status'], 30, 3);
}
public function add_admin_page() {
add_menu_page(
'User Ban Manager',
'User Ban',
'manage_options',
'user-ban-manager',
[$this, 'admin_page_content'],
'dashicons-shield-alt',
100
);
}
public function admin_page_content() {
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['ban_user'])) {
$this->ban_user(sanitize_text_field($_POST['username_or_email']), intval($_POST['ban_duration']));
}
?>
<div class="wrap">
<h1>Temporary User Ban</h1>
<form method="POST">
<label for="username_or_email">Username or Email:</label>
<input type="text" name="username_or_email" required>
<label for="ban_duration">Ban Duration (minutes):</label>
<input type="number" name="ban_duration" min="1" required>
<input type="submit" name="ban_user" value="Ban User" class="button button-primary">
</form>
<h2>Banned Users</h2>
<?php $this->display_banned_users(); ?>
</div>
<?php
}
private function ban_user($username_or_email, $ban_duration) {
$user = get_user_by('email', $username_or_email) ?: get_user_by('login', $username_or_email);
if ($user) {
$ban_end_time = current_time('timestamp') + ($ban_duration * 60);
update_user_meta($user->ID, '_ban_end_time', $ban_end_time);
echo '<div class="updated"><p>User has been banned.</p></div>';
} else {
echo '<div class="error"><p>User not found.</p></div>';
}
}
private function display_banned_users() {
$users = get_users(['meta_key' => '_ban_end_time', 'meta_compare' => '>=', 'meta_value' => current_time('timestamp')]);
if (empty($users)) {
echo '<p>No users are currently banned.</p>';
return;
}
echo '<ul>';
foreach ($users as $user) {
$ban_end_time = get_user_meta($user->ID, '_ban_end_time', true);
echo '<li>' . esc_html($user->user_login) . ' - Banned until ' . date('Y-m-d H:i:s', $ban_end_time) . '</li>';
}
echo '</ul>';
}
public function check_ban_status($user, $username, $password) {
if (is_wp_error($user)) {
return $user;
}
$ban_end_time = get_user_meta($user->ID, '_ban_end_time', true);
if ($ban_end_time && current_time('timestamp') < $ban_end_time) {
return new WP_Error('banned_user', 'You are banned until ' . date('Y-m-d H:i:s', $ban_end_time));
}
return $user;
}
}
new TemporaryUserBan();
How It Works:
Admin Menu: Adds a page in the WordPress admin where you can ban users by their username or email.
Temporary Ban: Stores the ban end time in user meta and automatically unbans them after the duration.
Login Check: Prevents banned users from logging in during the ban period.
Feedback: Lists all currently banned users with their ban expiration time.
Admin Menu: Adds a page in the WordPress admin where you can ban users by their username or email.
Temporary Ban: Stores the ban end time in user meta and automatically unbans them after the duration.
Login Check: Prevents banned users from logging in during the ban period.
Feedback: Lists all currently banned users with their ban expiration time.
Installation:
Create a new folder in yourwp-content/pluginsdirectory namedtemporary-user-ban.
Save the above code in a file namedtemporary-ban-users.phpinside that folder.
Activate the plugin through the WordPress admin.
Create a new folder in yourwp-content/pluginsdirectory namedtemporary-user-ban.
Save the above code in a file namedtemporary-ban-users.phpinside that folder.
Activate the plugin through the WordPress admin.
Let me know if you need further customizations!
Not yet included in a published release.