Here is a snippet that enables you to restrict certain usernames from being created by new users registering to your WordPress website.

Instructions
Add this code your theme function.php file.
*Remember to replace content of the “restricted array” with the usernames you want to restrict.

<?php
   function wpsnippet_validate_username($valid, $username) {
   $restricted = array('profile', '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) {
      $restricted[] = $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, $restricted )) $valid=false;
   if ($valid && strlen($username) < 5) $valid=false;
   return $valid;
   }
   add_filter('validate_username', 'wpsnippet_validate_username', 10, 2);

   function wpsnippet_registration_errors($errors) {
      if ( isset( $errors->errors['invalid_username'] ) )
         $errors->errors['invalid_username'][0] = __( 'ERROR: Invalid username.', 'wpsnippet' );
      return $errors;
   }
   add_filter('registration_errors', 'wpsnippet_registration_errors');
?>

If you want to use a plugin to achieve the same functionality, you can use Scott Reilly’s Restrict Usernames WordPress plugin.

Comments

  1. Hi,

    Thanks for your code it seems to be what i need exept that i want to restrict the e-mail with specific term.

    Im my case i don’t want user to create a account with the word “test” in their adress e-mail.

    Is it possible by changing a bit the code you left ? my website is https://www.francbio.com/

    Thanks for what you do

    1. @charles, you can just add whatever term you wish to exclude, in the restricted array: $restricted = array(‘test”, ‘test’2’);
      Be sure to remember to comma-separate all the terms, except for last one – or else you will get an error.

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

This could be the beginning of something great 🙌

"*" indicates required fields

Name*
Hidden
Hidden

By submitting this form: You agree to the processing of the submitted data in accordance with WP Dynamics Privacy Policy.

Contact Information
You can also write us an email or contact us by telephone.
Telephone +45 71 74 71 21

Support For support inquiries, please refer to our support section.

Recent Articles

Faust.js – A Headless Framework for WordPress

Faust.js is a Headless WordPress Framework that provides a set of tools for building front-end applications based on WordPress ...

How to Load (Enqueue) and Handle Assets and Dependencies in WordPress

In WordPress, instead of adding assets such as styles and JavaScript to the header, you can and should use ...

An Introduction to Google Tag Manager Server-side Tagging

WordPress has come a long way since its launch in 2003 - from a blogging tool to a full-fledge ...