
今まで知らなかったフィルターですが「illegal_user_logins」というアクションフィルターが WordPress4.4 から使えるようになっています。
ユーザー名リストを作ってこのフィルターを使うと、このリストにあるユーザー名はユーザー登録の時に「エラー: 許可されないユーザー名です。」と表示され、拒否できるようになります。
例えば「admin」や「root」等、わざわざ避けて管理者を作ったのに「納品したらいつのまにか 管理者で root が追加登録されている!」や 会員サイトのユーザー等でこれを回避する事ができそうです。
WordPressをシングルサイトで使う場合は「illegal_user_logins」フィルター用のユーザー名リストは 何も設定されていませんが、マルチサイトで利用する場合は設定されています。
調べてみるとマルチサイトでは「wpmu_validate_user_signup」という関数内でこれだけのブラックリストを設定していました。
$illegal_names = array( 'www', 'web', 'root', 'admin', 'main', 'invite', 'administrator' );
サンプルコード
以下のコードを my-plugin.php に張り付けてください。
/**
* 登録できないユーザー名を設定する
*
* @since 4.4.0
*
* @param array $usernames Array of blacklisted usernames.
* License: GPLv2 or later
*/
function nendebcom_illegal_user_logins( $illegal_logins ){
//Filters the list of blacklisted usernames.
$illegal_names = array( 'www', 'web', 'root', 'admin', 'main', 'invite', 'administrator' );
$illegal_logins = array_merge( $illegal_logins, $illegal_names );
return $illegal_logins;
}
add_filter( 'illegal_user_logins', 'nendebcom_illegal_user_logins' );
※$illegal_names に 拒否したいユーザー名を追加してください。
試しに新規登録してみるとこんな表示がでます。
【wp-login.php】

【管理画面】

おまけ 文字数の制限
ついでにユーザー名の文字数の制限もやっておきましょう。
ユーザー名の最大文字数は60文字以下で制限されていますが最小は1文字でも登録できてしまいます。
マルチサイトでは4文字以上となっていますのでこちらも4文字以上になるようにします。
以下のコードを my-plugin.php に張り付けてください。
/**
* ユーザー名の文字数の制限を4文字以上にする。(wp-login.php用)
*
* @since 2.1.0
*
* @param WP_Error $errors A WP_Error object containing any errors encountered
* during registration.
* @param string $sanitized_user_login User's username after it has been sanitized.
* @param string $user_email User's email.
* License: GPLv2 or later
*/
function nendebcom_registration_errors( $errors, $sanitized_user_login ){
if ( strlen( $sanitized_user_login ) < 4 ){
$errors->add('user_name', __( 'Username must be at least 4 characters.' ) );
}
return $errors;
}
add_filter( 'registration_errors', 'nendebcom_registration_errors', 10, 2 );
/**
* ユーザー名の文字数の制限を4文字以上にする。(管理画面用)
*
* @since 2.8.0
*
* @param WP_Error &$errors WP_Error object, passed by reference.
* @param bool $update Whether this is a user update.
* @param stdClass &$user User object, passed by reference.
* License: GPLv2 or later
*/
function nendebcom_user_profile_update_errors( $errors, $update, $user ){
if ( strlen( $user->user_login ) < 4 ){
$errors->add('user_name', __( 'Username must be at least 4 characters.' ) );
}
}
add_filter( 'user_profile_update_errors', 'nendebcom_user_profile_update_errors', 10, 3 );
試しに新規登録してみるとこんな表示がでます。
【wp-login.php】

【管理画面】

スポンサードリンク
参考
illegal_user_logins | WordPress Code Reference
https://developer.wordpress.org/reference/hooks/illegal_user_logins/
wpmu_validate_user_signup | WordPress Code Reference
https://developer.wordpress.org/reference/hooks/wpmu_validate_user_signup/
Plugin API/Action Reference/user profile update errors | WordPress Codex
https://codex.wordpress.org/Plugin_API/Action_Reference/user_profile_update_errors
Plugin API/Filter Reference/registration errors | WordPress Codex
https://codex.wordpress.org/Plugin_API/Filter_Reference/registration_errors