I'm working on airsoft website using woocommerce. One of my client's needs is to prevent a user to register if his name or mail is in a blacklist (I don't know yet if I'll use a csv file, a json or a simple array).

So I have to execute some validations before insert the user to the database. But I didn't succeed in finding the good hook.


2 Answers 2


If you examine the wp_insert_user() function, you can see there are a myriad of filters and actions that are called throughout the process. The first is a filter called pre_user_login on the username.

Line 1304 of wp-includes/user.php:

$user_login = apply_filters('pre_user_login', $user_login);

You could hook onto that and throw your own custom error message.

Edit: The email filter is just a few lines below that.

Second Edit: Added example

add_filter( 'pre_user_login' , 'my_username_block' );

function my_username_block( $user_login ) {

    $black_listed = array( 0 => 'satan' , 1 => 'hitler' );
    if( in_array( strtolower( $user_login ) , $black_listed ) ) {
        wp_die('That username is blacklisted');
    return $user_login;

add_filter( 'pre_user_email' , 'my_email_block' );

function my_email_block( $user_email ) {

    $black_listed_emails = array( 0 => 'iamaspammer@whatever.com' , 'imalsoaspammer@whatever.com' );
    if( in_array( strtolower( $user_email ) , $black_listed_emails ) ) {
        wp_die('That email address is blacklisted');
    return $user_email;
  • So I have to use "add_filter" function with this filter in my functions.php ?
    – aguidis
    Apr 11, 2013 at 10:32
  • I've updated my answer with an example using arrays. If you were to use a csv/json or wanted a more sophisticated error message, you'd need to adjust the script. This can go into your functions.php or you could make it a plugin. Apr 11, 2013 at 17:16
  • Sure thing, happy coding. Apr 12, 2013 at 16:27
  • I just noticed you did a test function on the user name but in my case I need to verify the complete name (lastname and firstname) instead of the username... Are "$user_firstname" and "user_lastname" the good variables ?
    – aguidis
    Apr 21, 2013 at 10:04
  • is there a mopre elegant way than wp_die() to achive this? i.e. if I want an error message to appear?
    – LobsterMan
    May 10, 2021 at 6:55

Perhaps use the user registration hook: http://codex.wordpress.org/Plugin_API/Action_Reference/user_register

This creates the user, and AFTERWARDS calls a function, but you could then immediate check the new user information against a blacklist and either remove the user or add a blacklisted custom field to his details.

  • I wanted to prevent an insertion without verifications. So this hook doesn't match with my need. That kind of user will not be able to do a reservation.
    – aguidis
    Apr 11, 2013 at 10:31

Not the answer you're looking for? Browse other questions tagged or ask your own question.