How to Block Email Domains on WordPress User Registration Without Plugins

In this guide, we’ll walk you through a simple yet powerful method to control user registrations on your WordPress site by blocking entire email domains – all without the need for plugins! Whether you’re focused on enhancing site security or refining user registration rules, our provided steps and code snippets offer a user-friendly solution. This approach empowers you to curate the registration process, simplifying it and granting you enhanced control. Optimize your WordPress site’s registration experience by following these steps for improved security and customization.

Before proceeding with any customizations in WordPress, it’s essential to set up a child theme. A child theme acts as a safe and efficient way to make modifications without affecting the parent theme. If you haven’t set up a child theme yet, follow this tutorial on How to Create a Child Theme for Customization. It will guide you through the process and ensure that your customizations remain intact even after theme updates.

Customize functions.php in Your Child Theme

To begin, open your WordPress child theme’s functions.php file. This file serves as the central hub for customizing your theme’s features. You can edit it directly within the Theme Editor in your WordPress dashboard or use an external code editor in combination with FTP (File Transfer Protocol) for more advanced editing. Insert the provided code snippet at the file’s end to implement the desired functionality.

// Block email domains on user registration
function custom_block_email_domains( $errors, $sanitized_user_login, $user_email ) {
	// Define blocked email domains
	$blocked_domains = apply_filters(
		'custom_blocked_domains',
		array(
			'example.net',
			'spam.com',
		)
	);

	// Get the user's email domain
	$user_domain = substr( strrchr( $user_email, '@' ), 1 ); // Extract domain from email

	// Check if the email domain is in the block list
	if ( in_array( $user_domain, $blocked_domains ) ) {
		// If blocked, set a custom error message
		$errors->add( 'registration-error', '<strong>Error:</strong> Users from this domain are blocked.' );
	}

	return $errors;
}

// Hook the function to the registration_errors action with a priority of 10 and three parameters
add_filter( 'registration_errors', 'custom_block_email_domains', 10, 3 );

Code language: PHP (php)

Explanation:

  • custom_block_email_domains: The function name that runs during user registration.
  • apply_filters: Allows developers to customize the list of blocked email domains using the ‘custom_blocked_domains’ filter.
  • substr and strrchr: Extracts the domain from the user’s email address.
  • add: Adds a custom error message if the user attempts to register with an email address from a blocked domain.
  • return $errors: Ensures the error message is applied.

Tailor the $blocked_domains array to include or exclude specific email domains based on your preferences.

Save and Test functions.php Changes

Implement the specified code snippets in your theme’s functions.php file, and be sure to save the file to validate the changes. After saving, take a moment to test the new features of your WordPress website. Verify the functionality by attempting to register with an email address from the blocked list. This simple yet vital procedure confirms that the custom code is active and fully functional on your site.

That’s it! By following this approach, you gain effective control over entire email domains during user registration in WordPress without the need for plugins. This method offers a straightforward and customizable solution for refining your site’s user management and security.

Leave your feedback and help us improve 🐶

We hope you found this article helpful! If you have any questions, feedback, or spot any errors, please let us know in the comments. Your input is valuable and helps us improve. If you liked this article, please consider sharing it with others. And if you really enjoyed it, you can show your support by buying us a cup of coffee ☕️ or donating via PayPal 💰.

More free knowledge, because why not?

Your thoughts matter, leave a reply 💬

Your email address will not be published. Required fields are marked *