Restrict sign-up to only specific domains

In Rentalhive, Is it possible to restrict the sign-up to just people having only .edu email ids or just specific college email ids (eg. @abc.edu) ? Because my business involves only students and I don’t want other than the selected colleges to sign up.

Thanks.

Hi @ishcap619,

Here’s the HivePress AI response:

You can restrict registration to .edu email domains by adding this PHP code snippet to your site:

add_filter(
    'hivepress/v1/forms/user_register',
    function( $form ) {
        $form['fields']['email']['_validate'] = function( $value ) {
            if ( ! preg_match( '/\.edu$/i', $value ) ) {
                return esc_html__( 'Only .edu email addresses are allowed to register.', 'hivepress' );
            }
        };
        
        return $form;
    },
    1000
);

How to add this snippet:

  1. Go to your WordPress dashboard
  2. Navigate to HivePress → Settings → Integrations
  3. Add the code to the Custom Code Snippets field
  4. Save changes

This code validates the email field during registration and only allows emails ending with .edu. If a user tries to register with a different email domain, they’ll see an error message.

To allow multiple educational domains (like .edu, .ac.uk, etc.), you can modify the pattern:

add_filter(
    'hivepress/v1/forms/user_register',
    function( $form ) {
        $form['fields']['email']['_validate'] = function( $value ) {
            if ( ! preg_match( '/\.(edu|ac\.uk|edu\.au)$/i', $value ) ) {
                return esc_html__( 'Only educational email addresses are allowed to register.', 'hivepress' );
            }
        };
        
        return $form;
    },
    1000
);

Note: I haven’t tested this myself.

​I hope this helps!

Cheers,
Chris :victory_hand:

Thank you so much!

Hi,

You can simplify it a little:

add_filter(
	'hivepress/v1/forms/user_register/errors',
	function ( $errors, $form ) {
		$email = $form->get_value( 'email' );
		if ( strpos( $email, '.edu' ) === false ) {
			$errors [] = 'Only .edu email addresses are allowed.';
		}
		return $errors;
	},
	1000,
	2
);

Hope this helps

Thank you!!