SOLVED: Blocking registrations from a specific email domain
-
As a follow up to Blocking registrations from a specific email domain (closed to replies) I wanted to share a solution I made for this that uses a filter from the core registration process (
registration_errors
).This filter can be used to just block the registrations (does not require WordFence in that case):
add_filter( 'registration_errors', 'disable_user_registration_for_email_domain', 10, 3 ); function disable_user_registration_for_email_domain ( $errors, $sanitized_user_login, $user_email ) { // only if it's an email address at all if ( ! is_email( $user_email ) ) { return $errors; } // get domain from email address $email_domain = substr( $user_email, strrpos( $user_email, '@' ) + 1 ); $block_domains = [ // partial domain names allowed (doesn't need to include TLD for example) 'spammersgalore.com', ]; foreach ( $block_domains as $domain_partial ) { if ( stripos( $email_domain, $domain_partial ) !== false ) { // throw registration error $errors->add( 'email_error', '<strong>ERROR</strong>: Registration not allowed.' ); } } return $errors; }
Or including the wordfence function to temporarily lock out the IP that was attempting the registration entirely:
add_filter( 'registration_errors', 'disable_user_registration_for_email_domain', 10, 3 ); function disable_user_registration_for_email_domain ( $errors, $sanitized_user_login, $user_email ) { // only execute when the relevant WordFence functions can be called if ( ! is_callable( 'wfUtils', 'getIP' ) || ! is_callable( 'wfBlock', 'isWhitelisted' ) || ! is_callable( 'wordfence', 'lockOutIP' )) { return $errors; } // only if it's an email address at all if ( ! is_email( $user_email ) ) { return $errors; } // get domain from email address $email_domain = substr( $user_email, strrpos( $user_email, '@' ) + 1 ); $block_domains = [ // partial domain names allowed (doesn't need to include TLD for example) 'spammersgalore.com', ]; foreach ( $block_domains as $domain_partial ) { if ( stripos( $email_domain, $domain_partial ) !== false ) { $IP = \wfUtils::getIP(); if ( \wfBlock::isWhitelisted( $IP ) ) { return $errors; // don't block whitelisted IPs } // lockout IP \wordfence::lockOutIP( $IP, "Registration attempt from blocked email domain {$domain_partial}" ); // throw registration error $errors->add( 'email_error', '<strong>ERROR</strong>: Registration not allowed.' ); } } return $errors; }
I hope this will be useful to someone. Tagging @ps6818 @nigelhay @jcpacelli @sidbicious @mouriana @dougrees from the previous topic in case they’re still interested in this
The page I need help with: [log in to see the link]
- The topic ‘SOLVED: Blocking registrations from a specific email domain’ is closed to new replies.