Prevent apostrophes in friend usernames and migrate existing ones#581
Prevent apostrophes in friend usernames and migrate existing ones#581
Conversation
akirk
left a comment
There was a problem hiding this comment.
Let's rather prevent usernames to have a quote in it in the first place and add a migration for usernames that happened to be created with them already.
Done! I've updated the approach:
The migration handles both real WordPress users and virtual Subscription users (stored as taxonomy terms). It only updates usernames that need sanitization and won't overwrite if the new username already exists. Commit: 3a451ec |
|
Please also ensure that in the js that generates the username from the display name no invalid chars make it into the username |
|
Let's also move the sanitize_user call into the get_by_username |
Co-authored-by: akirk <203408+akirk@users.noreply.github.com>
Co-authored-by: akirk <203408+akirk@users.noreply.github.com>
- Add username sanitization in User::create() and Subscription::create() - Add migration to sanitize existing usernames with special characters - Remove previous URL encoding approach per maintainer feedback Co-authored-by: akirk <203408+akirk@users.noreply.github.com>
3a451ec to
f8fb012
Compare
Test this PR in WordPress PlaygroundYou can test this pull request directly in WordPress Playground: This will install and activate the plugin with the changes from this PR. |
Usernames containing apostrophes (e.g., "John's Friend") caused "Invalid username" errors when accessing admin pages. Instead of handling these special characters in URLs, this fix prevents them from being created in usernames in the first place and migrates existing problematic usernames.
Changes
Prevention - Added automatic username sanitization:
All new friend usernames are automatically sanitized using
User::sanitize_username()which removes apostrophes and other special characters before user creation. This ensures URLs will always work correctly.Migration - Added
sanitize_usernamesmigration (version 4.2.1):How it works
Before (with apostrophe):
After (with sanitization):
Rationale
sanitize_user()already exists in the codebase and is designed to create safe usernames by removing special characters. By enforcing this sanitization at the creation point in bothUser::create()andSubscription::create(), we ensure no problematic usernames can be created. The migration handles any existing edge cases where unsanitized usernames were previously created.Files Modified
includes/class-user.php- Added sanitization in create methodincludes/class-subscription.php- Added sanitization in create methodincludes/class-migration.php- Added new migration registration and implementationOriginal prompt
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.