Skip to content

Instantly share code, notes, and snippets.

@afragen
Last active November 10, 2025 19:31
Show Gist options
  • Select an option

  • Save afragen/178e9bb282cb95c82ac6ade5331e4ee5 to your computer and use it in GitHub Desktop.

Select an option

Save afragen/178e9bb282cb95c82ac6ade5331e4ee5 to your computer and use it in GitHub Desktop.
Block new user registration by domain
<?php
/**
* Block User Registration by Domain
*
* Plugin Name: Block User Registration by Domain
* Plugin URI: https://gist.github.com/afragen/178e9bb282cb95c82ac6ade5331e4ee5
* Description: Block user registrations from specific email domains.
* Version: 0.5.2
* Author: Andy Fragen
* Author URI: https://thefragens.com
* License: MIT
* Requires at least: 6.6
* Requires PHP: 8.2
* Gist Plugin URI: https://gist.github.com/afragen/178e9bb282cb95c82ac6ade5331e4ee5
* Text Domain: block-user-registration-by-domain
*/
namespace Fragen\Block_User_Registration_By_Domain;
use WP_Error;
// Exit if accessed directly.
if (! defined('WPINC')) {
die;
}
/**
* Bootstrap.
*
* @return void
*/
function bootstrap(): void {
add_filter('registration_errors', __NAMESPACE__ . '\\block_user_registration_by_domain', 10, 3);
}
/**
* Check and block user registration by email domain.
*
* @param WP_Error $errors A WP_Error object containing any errors encountered
* during registration.
* @param string $sanitized_user_login User's username after it has been sanitized.
* @param string $user_email User's email.
*
* @return WP_Error
*/
function block_user_registration_by_domain($errors, $sanitized_user_login, $user_email): WP_Error {
// Add more domains as needed.
$blocked_domains = [
'socialpave.com',
'gemmasmith.co.uk',
];
$email_domain = substr(strrchr($user_email, "@"), 1);
foreach ($blocked_domains as $blocked) {
if (str_ends_with($email_domain, $blocked)) {
$errors->add('domain_blocked', __('Registrations from your email domain are not allowed.', 'block-user-registration-by-domain'));
error_log( "Blocked user registration attempt from email domain: $email_domain" );
break;
}
}
return $errors;
}
bootstrap();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment