Block specific users to create an account

Home » Snippets » Block specific users to create an account
0

Created with:

Custom snippet Generator

Visibility: 

public

Creator: chrmrtns@gmail.com

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project
				
function chrmrtns_block_email_addresses($user_id) {
    // Liste der zu blockierenden E-Mail-Adressen - List of blocked email addresses
    $blocked_emails = array(
        'block@example.com',
        'spam@example.net',
    );

    // Hole die E-Mail-Adresse des neu registrierten Benutzers - Get the email address of the newly registered user
    $user_email = get_userdata($user_id)->user_email;

    // Überprüfe, ob die E-Mail-Adresse in der Blockierliste enthalten ist - Check whether the e-mail address is included in the block list
    if (in_array($user_email, $blocked_emails)) {
        // Wenn die E-Mail-Adresse blockiert ist, entferne den neu registrierten Benutzer - If the e-mail address is blocked, remove the newly registered user
        wp_delete_user($user_id);
        wp_die('Sorry, this email address is blocked.');
    }
}

add_action('user_register', 'chrmrtns_block_email_addresses', 10, 1);
			

In this snippet, the function block_email_addresses is called when a user wants to register. The function checks whether the e-mail address entered by the user is included in the list of blocked e-mail addresses. If it is, the registration is canceled and an error message is displayed. You can update the list of email addresses to be blocked in the $blocked_emails variable and add more email addresses that you want to block. Just log into your site and edit your theme function. Add the snippet there and save. Make sure you use this feature with caution, as blocking email addresses may cause legitimate users to have problems registering. So be sure that you only block email addresses that you are sure represent spam or abuse.

Register an account to save your snippets or go Pro to get more features.