WordPress 禁止用户注册某些用户名

倡萌之前已经推荐过使用 Restrict Usernames 插件限制用户名使用空格,禁止注册某些用户名,禁止用户名包含某些字段,或者强制用户名必须包含某些字段等。

validate-username-wpdaxue_com

今天再补充一个简单点的方法,直接将下面的代码添加到主题的 functions.php 即可:

/**
 * WordPress 禁止<a href="https://www.wpdaxue.com/tag/%e7%94%a8%e6%88%b7%e6%b3%a8%e5%86%8c" title="查看与【用户注册】相关的文章" target="_blank" rel="noopener">用户注册</a>某些用户名
 * 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-&gt;post_name;
	}
	if(!$valid || is_user_logged_in() &amp;&amp; current_user_can('create_users') ) return $valid;
	$username = strtolower($username);
	if ($valid &amp;&amp; strpos( $username, ' ' ) !== false) $valid=false;
	if ($valid &amp;&amp; in_array( $username, $forbidden )) $valid=false;
	if ($valid &amp;&amp; strlen($username) &lt; 5) $valid=false;
	return $valid;
}
add_filter('validate_username', 'sozot_validate_username', 10, 2);
 
function sozot_registration_errors($errors) {
	if ( isset( $errors-&gt;errors['invalid_username'] ) )
		$errors-&gt;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 插件可能对某些注册插件或自定义注册表单可能不生效,比如本站目前使用的注册表单就不支持,很郁闷的说!

来源:

https://www.wpdaxue.com/wordpress-username-restrictions.html

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_33141.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?