今天再補充一個簡單點的方法,直接將下面的代碼添加到主題的 functions.php 即可:
來源于:
https://www.wpdaxue.com/wordPress-username-restrictions.html
/**
* WordPress 禁止用戶注冊某些用戶名
* https://www.wpdaxue.com/wordPress-username-restrictions.html
*/
function sozot_validate_username($valid, $username) {
$forbidden = array('directory', 'domain', 'download', 'downloads', 'edit', 'editor', 'email', 'ecommerce', 'forum', 'forums', 'favorite', 'feedback', 'follow', 'files', 'gadget', 'gadgets', 'games', 'guest', 'group', 'groups', 'homepage', 'hosting', 'hostname', 'httpd', 'https', 'information', 'image', 'images', 'index', 'invite', 'intranet', 'indice', 'iphone', 'javascript', 'knowledgebase', 'lists','websites', 'webmaster', 'workshop', 'yourname', 'yourusername', 'yoursite', 'yourdomain');
$pages = get_pages();
foreach ($pages as $page) {
$forbidden[] = $page->post_name;
}
if(!$valid || is_user_logged_in() && current_user_can('create_users') ) return $valid;
$username = strtolower($username);
if ($valid && strpos( $username, ' ' ) !== false) $valid=false;
if ($valid && in_array( $username, $forbidden )) $valid=false;
if ($valid && strlen($username) < 5) $valid=false;
return $valid;
}
add_filter('validate_username', 'sozot_validate_username', 10, 2);
?
function sozot_registration_errors($errors) {
if ( isset( $errors->errors['invalid_username'] ) )
$errors->errors['invalid_username'][0] = __( '錯誤:該用戶名不允許注冊!', 'sozot' );
return $errors;
}
add_filter('registration_errors', 'sozot_registration_errors');
你只需將禁止注冊的用戶名添加到第 6 行的數組即可。
參考資料:https://sozot.com/wordpress-username-restrictions-without-a-plugin/
PS:經過倡萌測試,本文的代碼和?Restrict Usernames?插件可能對某些注冊插件或自定義注冊表單可能不生效,比如本站目前使用的注冊表單就不支持,很郁悶的說!