ID ); $old_username = ( $old_user ) ? $old_user->user_login : ''; if ( $new_username !== $old_username ) { if ( ! validate_username( $new_username ) ) { /* translators: Error message when the entered username is invalid. */ $errors->add( 'user_login', __( 'ERROR: This username is invalid.', 'allow-username-edit' ) ); return; } // Prevent email-like usernames for security. if ( strpos( $new_username, '@' ) !== false ) { /* translators: Error message when the username contains @ symbol. */ $errors->add( 'user_login', __( 'ERROR: Usernames cannot contain "@" symbol or resemble email addresses.', 'allow-username-edit' ) ); return; } $existing_id = username_exists( $new_username ); if ( $existing_id && (int) $existing_id !== (int) $user->ID ) { /* translators: Error message when the entered username is already taken by another user. */ $errors->add( 'user_login', __( 'ERROR: This username is already registered.', 'allow-username-edit' ) ); return; } } } add_action( 'user_profile_update_errors', 'ueoplug2025_validate_username_change', 10, 3 ); function ueoplug2025_update_username( $user_id, $old_user_data ) { static $updating = false; if ( $updating ) { return; } $updating = true; if ( ! ueoplug2025_check_permissions_and_nonce() ) { $updating = false; return; } if ( ! isset( $_POST['user_login'] ) ) { $updating = false; return; } $new_username = sanitize_user( wp_unslash( $_POST['user_login'] ), true ); $old_username = isset( $old_user_data->user_login ) ? $old_user_data->user_login : ''; if ( $new_username !== $old_username ) { if ( ! validate_username( $new_username ) ) { $updating = false; return; } $existing_id = username_exists( $new_username ); if ( $existing_id && (int) $existing_id !== (int) $user_id ) { $updating = false; return; } // Direct database update is required because wp_update_user() does not support updating user_login global $wpdb; $result = $wpdb->update( $wpdb->users, array( 'user_login' => $new_username ), array( 'ID' => $user_id ), array( '%s' ), array( '%d' ) ); if ( false === $result ) { $updating = false; return; } // Clear user cache to ensure changes are reflected clean_user_cache( $user_id ); } $updating = false; } add_action( 'profile_update', 'ueoplug2025_update_username', 10, 2 ); // Update validation to check for activate checkbox. function ueoplug2025_validate_username_change_with_checkbox( $errors, $update, $user ) { if ( ! current_user_can( 'edit_users' ) ) { return; } // Verify nonce for security. if ( ! isset( $_POST['ueoplug2025_nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['ueoplug2025_nonce'] ) ), 'ueoplug2025_update_user' ) ) { return; } // Only validate if the username was actually changed. $new_username = isset( $_POST['user_login'] ) ? sanitize_user( wp_unslash( $_POST['user_login'] ), true ) : ''; $old_user = get_userdata( $user->ID ); $old_username = ( $old_user ) ? $old_user->user_login : ''; if ( $new_username === $old_username ) { return; } if ( ! isset( $_POST['ueo_activate_username_edit'] ) || '1' !== $_POST['ueo_activate_username_edit'] ) { /* translators: Error message when the activation checkbox is not checked. */ $errors->add( 'user_login', __( 'ERROR: You must activate username editing by checking the checkbox above before making changes.', 'allow-username-edit' ) ); } } add_action( 'user_profile_update_errors', 'ueoplug2025_validate_username_change_with_checkbox', 5, 3 ); // Add settings page. function ueoplug2025_add_settings_page() { /* translators: Page title for the settings page. */ add_options_page( __( 'Username Edit Settings', 'allow-username-edit' ), /* translators: Menu title for the settings page. */ __( 'Username Edit', 'allow-username-edit' ), 'manage_options', 'ueoplug2025-settings', 'ueoplug2025_render_settings_page' ); } add_action( 'admin_menu', 'ueoplug2025_add_settings_page' ); // Render settings page. function ueoplug2025_render_settings_page() { ?>

user_login; } else { // For user-edit.php, we need to get the user ID from the URL. $user_id = isset( $_GET['user_id'] ) ? absint( $_GET['user_id'] ) : $current_user->ID; // Verify the current user can edit this specific user for security. if ( ! current_user_can( 'edit_user', $user_id ) ) { $user_id = $current_user->ID; } $user_info = get_userdata( $user_id ); $original_username = $user_info ? $user_info->user_login : ''; } // Prepare texts $text_activated = esc_js( __( 'Username editing is now activated. You can change the username above.', 'allow-username-edit' ) ); $text_protected = esc_js( __( 'Username is protected. Check the box to enable editing.', 'allow-username-edit' ) ); $text_confirm = esc_js( __( 'Are you sure you want to change the username? This action may require the user to log in again.', 'allow-username-edit' ) ); $text_email_warning = esc_js( __( 'Warning: Use of email addresses in usernames is discouraged for security reasons.', 'allow-username-edit' ) ); // Add translation labels for all translatable texts to translation template /* translators: Message shown when username editing is activated. */ __( 'Username editing is now activated. You can change the username above.', 'allow-username-edit' ); /* translators: Message shown when username editing is protected. */ __( 'Username is protected. Check the box to enable editing.', 'allow-username-edit' ); /* translators: Confirmation message shown before username change. */ __( 'Are you sure you want to change the username? This action may require the user to log in again.', 'allow-username-edit' ); /* translators: Warning message shown when username contains @ symbol. */ __( 'Warning: Use of email addresses in usernames is discouraged for security reasons.', 'allow-username-edit' ); /* translators: Error message when username contains @ symbol. */ __( 'Usernames cannot contain "@" symbol or resemble email addresses.', 'allow-username-edit' ); /* translators: Label for the username editing section. */ __( 'Edit Username', 'allow-username-edit' ); /* translators: Label for the activate checkbox. */ __( 'Activate username editing', 'allow-username-edit' ); /* translators: Error message when username is invalid. */ __( 'This username is invalid.', 'allow-username-edit' ); /* translators: Error message when username already exists. */ __( 'This username is already registered.', 'allow-username-edit' ); /* translators: Error message when checkbox is not checked. */ __( 'You must activate username editing by checking the checkbox above before making changes.', 'allow-username-edit' ); /* translators: Page title for settings page. */ __( 'Username Edit Settings', 'allow-username-edit' ); /* translators: Section heading for plugin information. */ __( 'Plugin Information', 'allow-username-edit' ); /* translators: Label for plugin name. */ __( 'Plugin Name:', 'allow-username-edit' ); /* translators: Plugin name. */ __( 'Allow Username Edit', 'allow-username-edit' ); /* translators: Label for version. */ __( 'Version:', 'allow-username-edit' ); /* translators: Label for description. */ __( 'Description:', 'allow-username-edit' ); /* translators: Plugin description. */ __( 'This plugin allows administrators to safely edit usernames of registered users with built-in protection against accidental changes.', 'allow-username-edit' ); /* translators: Label for author. */ __( 'Author:', 'allow-username-edit' ); /* translators: Label for author URI. */ __( 'Author URI:', 'allow-username-edit' ); /* translators: Section heading for usage instructions. */ __( 'Usage Instructions', 'allow-username-edit' ); /* translators: Step 1 of usage instructions. */ __( 'Navigate to Users → All Users', 'allow-username-edit' ); /* translators: Step 2 of usage instructions. */ __( 'Click "Edit" on any user', 'allow-username-edit' ); /* translators: Step 3 of usage instructions. */ __( 'Check the "Activate username editing" checkbox above the username field', 'allow-username-edit' ); /* translators: Step 4 of usage instructions. */ __( 'Edit the username as needed', 'allow-username-edit' ); /* translators: Step 5 of usage instructions. */ __( 'Confirm the change when prompted', 'allow-username-edit' ); /* translators: Section heading for support. */ __( 'Support the Plugin', 'allow-username-edit' ); /* translators: Text encouraging donation. */ __( 'If you find this plugin useful, please consider making a donation to support continued development and maintenance.', 'allow-username-edit' ); /* translators: Button text for donation. */ __( 'Donate via PayPal', 'allow-username-edit' ); $label_th = esc_html__( 'Edit Username', 'allow-username-edit' ); $label_cb = esc_html__( 'Activate username editing', 'allow-username-edit' ); $checkbox_row_html = '' . '' . '' . '' . '' . ''; // Enqueue JS wp_enqueue_script( 'ueoplug2025-admin-js', UEOPLUG2025_PLUGIN_URL . 'assets/js/admin.js', array( 'jquery' ), UEOPLUG2025_VERSION, true ); // Localize wp_localize_script( 'ueoplug2025-admin-js', 'ueoplug2025_vars', array( 'original_username' => $original_username, 'text_activated' => $text_activated, 'text_protected' => $text_protected, 'text_confirm' => $text_confirm, 'text_email_warning' => $text_email_warning, 'checkbox_row_html' => $checkbox_row_html, ) ); } } add_action( 'admin_enqueue_scripts', 'ueoplug2025_enqueue_admin_assets' );