Allow only specific email domains in registration

Hello,

I need to implement a spam filter on the registration form to prevent “invalid” domain name, incomplate email and blocked user. How can i do it?

I tried to do some quick snippets but wasn working.

add_filter( 'hp_user_register_args', 'block_user_by_email_or_domain', 10, 1 );
function block_user_by_email_or_domain( $args ) {

    // Email esatte da bloccare
    $blocked_emails = array(
        'utente@example.com',
        'spamuser@gmail.com'
    );

    // Domini email da bloccare
    $blocked_domains = array(
        'mailinator.com',
        'tempmail.com',
        '10minutemail.com',
        'dispostable.com'
    );

    $email = isset($args['user_email']) ? strtolower(trim($args['user_email'])) : '';

    // Controlla email specifiche
    if ( in_array( $email, $blocked_emails ) ) {
        throw new \Exception( 'Registrazione non consentita con questa email.' );
    }

    // Estrai e controlla il dominio
    if ( strpos($email, '@') !== false ) {
        $domain = substr(strrchr($email, "@"), 1);
        if ( in_array( $domain, $blocked_domains ) ) {
            throw new \Exception( 'Registrazione non consentita da questo dominio email.' );
        }
    }

    return $args;
}

5 posts were merged into an existing topic: Registration only for specific domains (like for instance @company.com)