Created
July 2, 2026 11:47
-
-
Save annuman97/cf2b0b5b242668a0f225fe513d915a8a to your computer and use it in GitHub Desktop.
Temporary FluentCommunity workaround to generate friendly anonymous WordPress usernames during default signup, helping prevent email-derived usernames from being exposed in mentions or profile UI.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| defined('ABSPATH') || exit; | |
| /** | |
| * Modify FluentCommunity signup fields. | |
| */ | |
| add_filter('fluent_community/auth/signup_fields', function ($fields) { | |
| if (isset($fields['full_name'])) { | |
| $fields['full_name']['label'] = 'Public Display Name'; | |
| $fields['full_name']['placeholder'] = 'Example: Optimisticflamingo24'; | |
| } | |
| if (isset($fields['username'])) { | |
| // Do not disable/remove this field. FluentCommunity still validates username. | |
| unset($fields['username']['disabled']); | |
| unset($fields['username']['readonly']); | |
| $fields['username']['required'] = false; | |
| $fields['username']['label'] = ''; | |
| $fields['username']['placeholder'] = ''; | |
| $fields['username']['value'] = ''; | |
| $fields['username']['input_class'] = 'fcom-anonymous-generated-username'; | |
| } | |
| return $fields; | |
| }, 20); | |
| /** | |
| * Server-side enforcement. | |
| * This replaces the submitted username before FluentCommunity creates the WP user. | |
| */ | |
| add_action('wp_ajax_nopriv_fcom_user_registration', 'fcom_force_friendly_anonymous_signup_username', 0); | |
| add_action('wp_ajax_fcom_user_registration', 'fcom_force_friendly_anonymous_signup_username', 0); | |
| function fcom_force_friendly_anonymous_signup_username() | |
| { | |
| $username = fcom_generate_friendly_anonymous_username(); | |
| $_POST['username'] = $username; | |
| $_REQUEST['username'] = $username; | |
| } | |
| /** | |
| * Frontend CSS/JS for FluentCommunity registration form. | |
| */ | |
| add_action('wp_footer', 'fcom_anonymous_signup_frontend_script', 999); | |
| add_action('fluent_community/headless/footer', 'fcom_anonymous_signup_frontend_script', 999); | |
| function fcom_anonymous_signup_frontend_script() | |
| { | |
| static $printed = false; | |
| if ($printed) { | |
| return; | |
| } | |
| $printed = true; | |
| ?> | |
| <style> | |
| #fcom_group_username, | |
| .fcom-anonymous-generated-username { | |
| display: none !important; | |
| } | |
| </style> | |
| <script> | |
| (function () { | |
| var adjectives = [ | |
| 'bright', 'calm', 'gentle', 'happy', 'kind', | |
| 'lucky', 'quiet', 'sunny', 'clever', 'brave', | |
| 'cozy', 'wise', 'merry', 'fresh', 'soft', | |
| 'hopeful', 'peaceful', 'steady', 'warm', 'bold' | |
| ]; | |
| var animals = [ | |
| 'panda', 'otter', 'robin', 'turtle', 'owl', | |
| 'fox', 'koala', 'dolphin', 'sparrow', 'rabbit', | |
| 'beaver', 'badger', 'falcon', 'penguin', 'whale', | |
| 'deer', 'lynx', 'swan', 'seal', 'wren' | |
| ]; | |
| function randomItem(items) { | |
| return items[Math.floor(Math.random() * items.length)]; | |
| } | |
| function friendlyUsername() { | |
| var number = Math.floor(Math.random() * 990) + 10; | |
| return randomItem(adjectives) + '_' + randomItem(animals) + '_' + number; | |
| } | |
| function ensureAnonymousUsername(form) { | |
| if (!form) { | |
| return; | |
| } | |
| var input = form.querySelector('input[name="username"]'); | |
| if (!input) { | |
| input = document.createElement('input'); | |
| input.type = 'hidden'; | |
| input.name = 'username'; | |
| form.appendChild(input); | |
| } | |
| input.disabled = false; | |
| if (!input.value || input.value.length < 4) { | |
| input.value = friendlyUsername(); | |
| } | |
| } | |
| document.addEventListener('DOMContentLoaded', function () { | |
| var form = document.getElementById('fcom_user_registration_form'); | |
| ensureAnonymousUsername(form); | |
| }); | |
| document.addEventListener('submit', function (event) { | |
| var form = event.target && event.target.closest | |
| ? event.target.closest('#fcom_user_registration_form') | |
| : null; | |
| ensureAnonymousUsername(form); | |
| }, true); | |
| })(); | |
| </script> | |
| <?php | |
| } | |
| /** | |
| * Generate friendly anonymous usernames. | |
| * | |
| * Example: | |
| * calm_panda_42 | |
| * bright_otter_317 | |
| * gentle_robin_88 | |
| */ | |
| function fcom_generate_friendly_anonymous_username() | |
| { | |
| $adjectives = [ | |
| 'bright', 'calm', 'gentle', 'happy', 'kind', | |
| 'lucky', 'quiet', 'sunny', 'clever', 'brave', | |
| 'cozy', 'wise', 'merry', 'fresh', 'soft', | |
| 'hopeful', 'peaceful', 'steady', 'warm', 'bold' | |
| ]; | |
| $animals = [ | |
| 'panda', 'otter', 'robin', 'turtle', 'owl', | |
| 'fox', 'koala', 'dolphin', 'sparrow', 'rabbit', | |
| 'beaver', 'badger', 'falcon', 'penguin', 'whale', | |
| 'deer', 'lynx', 'swan', 'seal', 'wren' | |
| ]; | |
| for ($i = 0; $i < 100; $i++) { | |
| $adjective = $adjectives[array_rand($adjectives)]; | |
| $animal = $animals[array_rand($animals)]; | |
| $number = wp_rand(10, 999); | |
| $username = sanitize_user($adjective . '_' . $animal . '_' . $number, true); | |
| // Keep it FluentCommunity-friendly. | |
| $username = substr($username, 0, 30); | |
| if (fcom_friendly_anonymous_username_is_available($username)) { | |
| return $username; | |
| } | |
| } | |
| // Fallback. | |
| for ($i = 0; $i < 20; $i++) { | |
| $username = sanitize_user('member_' . wp_rand(100000, 999999), true); | |
| if (fcom_friendly_anonymous_username_is_available($username)) { | |
| return $username; | |
| } | |
| } | |
| return sanitize_user('member_' . time() . wp_rand(100, 999), true); | |
| } | |
| /** | |
| * Check username availability in WordPress and FluentCommunity. | |
| */ | |
| function fcom_friendly_anonymous_username_is_available($username) | |
| { | |
| if (!$username || strlen($username) < 4) { | |
| return false; | |
| } | |
| if (username_exists($username)) { | |
| return false; | |
| } | |
| if (class_exists('\FluentCommunity\App\Services\ProfileHelper')) { | |
| try { | |
| if (!\FluentCommunity\App\Services\ProfileHelper::isUsernameAvailable($username)) { | |
| return false; | |
| } | |
| } catch (Throwable $e) { | |
| // If FluentCommunity check fails, continue with WordPress username check. | |
| } | |
| } | |
| return true; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment