WordPress code update user name trong Admin CMS không sử dụng plugin
<?php // Add username field to the profile page function allow_username_editing( $user ) { ?> <h3>Đổi tên Tác giả</h3> <table class="form-table"> <tr> <th><label for="username">Tên tác giả</label></th> <td> <input type="text" name="username" id="username" value="<?php echo esc_attr( $user->user_login ); ?>" class="regular-text" /> <span class="description">Nhập tên tác giả các ký tự từ a-z viết thường.</span> </td> </tr> </table> <?php } add_action( 'show_user_profile', 'allow_username_editing' ); add_action( 'edit_user_profile', 'allow_username_editing' ); // Save the new username function save_edited_username( $user_id ) { if ( ! current_user_can( 'edit_user', $user_id ) ) { return false; } if ( isset( $_POST['username'] ) && ! empty( $_POST['username'] ) ) { $username = sanitize_user( $_POST['username'] ); // Check if the username is already taken if ( username_exists( $username ) ) { add_action( 'user_profile_update_errors', function( $errors ) { $errors->add( 'username_error', __( 'This username is already taken. Please choose another one.' ) ); } ); } else { // Update the username in the database global $wpdb; $wpdb->update( $wpdb->users, array( 'user_login' => $username ), array( 'ID' => $user_id ) ); clean_user_cache( $user_id ); } } } add_action( 'personal_options_update', 'save_edited_username' ); add_action( 'edit_user_profile_update', 'save_edited_username' );