admin管理员组文章数量:1130349
I've been looking and looking for a solution for my problem but i have been unable to find anything.
I have found a plugin that allow me to restrict certain usernames, I found even a function but there is nothing that allows me to restrict partially matching usernames, there is nothing to prevent users to register under names like "joeadmin" or "seanadmin" and etc.
Is there anything that can be done to prevent users to register anything that contain "admin" and other prohibited words?
I've been looking and looking for a solution for my problem but i have been unable to find anything.
I have found a plugin that allow me to restrict certain usernames, I found even a function but there is nothing that allows me to restrict partially matching usernames, there is nothing to prevent users to register under names like "joeadmin" or "seanadmin" and etc.
Is there anything that can be done to prevent users to register anything that contain "admin" and other prohibited words?
Share Improve this question asked Feb 23, 2017 at 14:13 Gabriel DiLaurentisGabriel DiLaurentis 235 bronze badges2 Answers
Reset to default 2There's a validate_username filter hook that is used by validate_user() which is in turn used by register_new_user().
So you can disallow usernames containing admin or other prohibited terms:
add_filter( 'validate_username', 'wpse257667_check_username', 10, 2 );
function wpse257667_check_username( $valid, $username ) {
// This array can grow as large as needed.
$disallowed = array( 'admin', 'other_disallowed_string' );
foreach( $disallowed as $string ) {
// If any disallowed string is in the username,
// mark $valid as false.
if ( $valid && false !== strpos( $username, $string ) ) {
$valid = false;
}
}
return $valid;
}
You can add this code to your theme's functions.php file or (preferably) make it a plugin.
References
validate_usernamefilter hookvalidate_user()register_new_user()- Writing a plugin
@pat-j beat me to it by a minute, but I'll elaborate that you can use a reg ex as well:
add_filter( 'validate_username', 'wpse257667_check_username', 10, 2 );
function wpse257667_check_username( $valid, $username ) {
if($valid){
$matches = null;
$returnValue = preg_match('/(admin)/i', $username, $matches);
return empty($matches) ? true : false;
}
}
I've been looking and looking for a solution for my problem but i have been unable to find anything.
I have found a plugin that allow me to restrict certain usernames, I found even a function but there is nothing that allows me to restrict partially matching usernames, there is nothing to prevent users to register under names like "joeadmin" or "seanadmin" and etc.
Is there anything that can be done to prevent users to register anything that contain "admin" and other prohibited words?
I've been looking and looking for a solution for my problem but i have been unable to find anything.
I have found a plugin that allow me to restrict certain usernames, I found even a function but there is nothing that allows me to restrict partially matching usernames, there is nothing to prevent users to register under names like "joeadmin" or "seanadmin" and etc.
Is there anything that can be done to prevent users to register anything that contain "admin" and other prohibited words?
Share Improve this question asked Feb 23, 2017 at 14:13 Gabriel DiLaurentisGabriel DiLaurentis 235 bronze badges2 Answers
Reset to default 2There's a validate_username filter hook that is used by validate_user() which is in turn used by register_new_user().
So you can disallow usernames containing admin or other prohibited terms:
add_filter( 'validate_username', 'wpse257667_check_username', 10, 2 );
function wpse257667_check_username( $valid, $username ) {
// This array can grow as large as needed.
$disallowed = array( 'admin', 'other_disallowed_string' );
foreach( $disallowed as $string ) {
// If any disallowed string is in the username,
// mark $valid as false.
if ( $valid && false !== strpos( $username, $string ) ) {
$valid = false;
}
}
return $valid;
}
You can add this code to your theme's functions.php file or (preferably) make it a plugin.
References
validate_usernamefilter hookvalidate_user()register_new_user()- Writing a plugin
@pat-j beat me to it by a minute, but I'll elaborate that you can use a reg ex as well:
add_filter( 'validate_username', 'wpse257667_check_username', 10, 2 );
function wpse257667_check_username( $valid, $username ) {
if($valid){
$matches = null;
$returnValue = preg_match('/(admin)/i', $username, $matches);
return empty($matches) ? true : false;
}
}
本文标签: user registrationRestrict partially matching usernames
版权声明:本文标题:user registration - Restrict partially matching usernames 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749154947a2324451.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论