Blog Name Restrictions Override plugin. Author: Daniel Westermann-Clark Author URI: http://danieltwc.com/ */ class NetworkUsernameRestrictionsOverridePlugin { var $db_version = 2; var $option_name = 'network_username_restrictions_override_options'; var $options; var $default_options = array( 'allow_email_addresses' => false, 'allow_numeric' => false, 'allow_hyphens' => false, 'allow_periods' => false, 'allow_underscores' => false, 'allow_uppercase' => false, 'min_length' => 4, ); function NetworkUsernameRestrictionsOverridePlugin() { add_action('init', array($this, 'init')); } function init() { $this->options = get_site_option($this->option_name); if (is_admin()) { add_action('admin_init', array($this, 'check_options')); } add_action('wpmu_options', array($this, 'display_options')); add_action('update_wpmu_options', array($this, 'update_options')); add_filter('wpmu_validate_user_signup', array($this, 'validate_username')); // WordPress alters usernames in ways that don't match our overrides add_filter('sanitize_user', array($this, 'sanitize_username'), 10, 3); } /* * Override errors generated by WordPress due to network username * restrictions that are sort of insane. */ function validate_username($result) { if (! is_wp_error($result['errors'])) { return $result; } $username = $result['user_name']; // Copy any error messages that have not been overridden $new_errors = new WP_Error(); $errors = $result['errors']; $codes = $errors->get_error_codes(); foreach ($codes as $code) { $messages = $errors->get_error_messages($code); if ($code == 'user_name') { foreach ($messages as $message) { if ($message == __('Only lowercase letters (a-z) and numbers are allowed.')) { // Check if we allow email addresses; otherwise, check against character overrides if (is_email($username)) { if (! $this->options['allow_email_addresses']) { $new_errors->add($code, $message); } } else { $allowed_characters = ''; if ($this->options['allow_hyphens']) $allowed_characters .= '-'; if ($this->options['allow_underscores']) $allowed_characters .= '_'; if ($this->options['allow_periods']) $allowed_characters .= '.'; if ($this->options['allow_uppercase']) $allowed_characters .= 'A-Z'; preg_match('/[' . $allowed_characters . 'a-z0-9]+/', $username, $maybe); if ($username != $maybe[0]) { $new_errors->add($code, $message); } } } elseif ($message == __('Sorry, usernames may not contain the character “_”!')) { // Need a separate check against underscores due to username validation in ms-functions.php if (is_email($username) && ! $this->options['allow_underscores']) { $new_errors->add($code, $message); } elseif (! $this->options['allow_underscores']) { $new_errors->add($code, $message); } } elseif ($message == __('Sorry, usernames must have letters too!')) { // Check if we're allowing usernames that are only numbers if (! $this->options['allow_numeric']) { $new_errors->add($code, $message); } } elseif ($message == __('Username must be at least 4 characters.')) { // Check the username length if (strlen($username) < $this->options['min_length']) { $new_errors->add($code, $message); } } else { // Restore other username errors $new_errors->add($code, $message); } } } else { // Restore any other errors foreach ($messages as $message) { $new_errors->add($code, $message); } } } /* * If the super admin sets the minimum length to longer than four * characters, wpmu_validate_user_signup won't give us an error */ if (strlen($username) < $this->options['min_length']) { $new_errors->add('user_name', __(sprintf('Username must be at least %d characters.', $this->options['min_length']))); } $result['errors'] = $new_errors; return $result; } /* * Email addresses can contain characters not allowed in the strict set, * such as '+'. */ function sanitize_username($username, $raw_username, $strict) { if ($this->options['allow_email_addresses'] && is_email($raw_username)) { $username = $raw_username; } return $username; } /* * Check the options currently in the database and upgrade if necessary. */ function check_options() { if ($this->options === false || ! isset($this->options['db_version']) || $this->options['db_version'] < $this->db_version) { if (! is_array($this->options)) { $this->options = array(); } $current_db_version = isset($this->options['db_version']) ? $this->options['db_version'] : 0; $this->upgrade($current_db_version); $this->options['db_version'] = $this->db_version; update_site_option($this->option_name, $this->options); } } /* * Upgrade options as needed depending on the current database version. */ function upgrade($current_db_version) { if ($current_db_version < 1) { foreach ($this->default_options as $key => $value) { // Handle migrating existing options from before we stored a db_version if (! isset($this->options[$key])) { $this->options[$key] = $value; } } } } /* * Upgrade options as needed depending on the current database version. */ function display_options() { ?>

Username Restrictions Settings

By default, WordPress limits network usernames to only lowercase letters and numbers. Enable additional formats or specific characters below.

Username format:
Username character restrictions:



Note: WordPress converts usernames to lowercase. This option simply makes data entry easier.

(Default: 4)
options[$flag] = (bool) $submitted[$flag]; } $this->options['min_length'] = (int) $submitted['min_length']; update_site_option($this->option_name, $this->options); } } $network_username_restrictions_override_plugin = new NetworkUsernameRestrictionsOverridePlugin(); ?>