File: /var/www/html/wp-content/plugins.bak/bulk-update-users.php
<?php
/*
* Plugin Name: Bulk User Update
* Description: A simple admin page to bulk update user passwords, emails, and names.
* Author: Grok
* Version: 1.0
*/
// 添加管理菜单
add_action('admin_menu', 'bulk_update_users_menu');
function bulk_update_users_menu() {
add_menu_page(
'Bulk Update Users',
'Bulk Update Users',
'manage_options',
'bulk-update-users',
'bulk_update_users_page',
'dashicons-admin-users',
80
);
}
// 管理页面内容
function bulk_update_users_page() {
// 检查用户权限
if (!current_user_can('manage_options')) {
wp_die('You do not have permission to access this page.');
}
// 处理表单提交
$message = '';
if (isset($_POST['bulk_update_users_submit']) && check_admin_referer('bulk_update_users_nonce')) {
$new_password = sanitize_text_field($_POST['new_password']);
$email_suffix = sanitize_text_field($_POST['email_suffix']);
$name_prefix = sanitize_text_field($_POST['name_prefix']);
$selected_role = sanitize_text_field($_POST['user_role']);
// 获取用户
$args = array();
if ($selected_role !== 'all') {
$args['role'] = $selected_role;
}
$users = get_users($args);
$updated_count = 0;
foreach ($users as $user) {
$update_data = array('ID' => $user->ID);
// 更新密码
if (!empty($new_password)) {
wp_set_password($new_password, $user->ID);
}
// 更新邮箱
if (!empty($email_suffix)) {
$new_email = $user->user_login . $email_suffix;
$update_data['user_email'] = sanitize_email($new_email);
}
// 更新姓名
if (!empty($name_prefix)) {
$update_data['display_name'] = $name_prefix . $user->user_login;
$update_data['first_name'] = $name_prefix . $user->first_name;
$update_data['last_name'] = $name_prefix . $user->last_name;
}
// 执行更新
if (count($update_data) > 1) { // 确保有除 ID 外的更新内容
$result = wp_update_user($update_data);
if (!is_wp_error($result)) {
$updated_count++;
}
}
mentions:
}
$message = "Updated $updated_count users successfully.";
}
// 获取所有角色
global $wp_roles;
$roles = $wp_roles->get_names();
?>
<div class="wrap">
<h1>Bulk Update Users</h1>
<?php if ($message) : ?>
<div class="notice notice-success"><p><?php echo esc_html($message); ?></p></div>
<?php endif; ?>
<form method="post" action="">
<?php wp_nonce_field('bulk_update_users_nonce'); ?>
<table class="form-table">
<tr>
<th><label for="new_password">New Password</label></th>
<td><input type="text" name="new_password" id="new_password" class="regular-text" placeholder="Leave blank to skip"></td>
</tr>
<tr>
<th><label for="email_suffix">Email Suffix</label></th>
<td><input type="text" name="email_suffix" id="email_suffix" class="regular-text" placeholder="e.g., @newdomain.com"></td>
</tr>
<tr>
<th><label for="name_prefix">Name Prefix</label></th>
<td><input type="text" name="name_prefix" id="name_prefix" class="regular-text" placeholder="e.g., Student_"></td>
</tr>
<tr>
<th><label for="user_role">User Role</label></th>
<td>
<select name="user_role" id="user_role">
<option value="all">All Roles</option>
<?php foreach ($roles as $role_key => $role_name) : ?>
<option value="<?php echo esc_attr($role_key); ?>"><?php echo esc_html($role_name); ?></option>
<?php endforeach; ?>
</select>
</td>
</tr>
</table>
<?php submit_button('Update Users', 'primary', 'bulk_update_users_submit'); ?>
</form>
</div>
<?php
}
?>