Restrict user registration to emails on a single domain

Question

I need to allow user registrations only for those with email addresses on a single domain. I have tested multiple regular expressions and this one works well in a regex sandbox environment, but as soon as I put it into my functions.php file, it simply rejects all registrations – even email addresses on the correct domain (with the error message below, so it’s definitely this conditional that’s breaking it). Am I:
a) actually doing something stupid in the regex, even tho the online regex testers say it’s doing what I want?
b) using a regex syntax that WordPress doesn’t support?
c) screwing up something in the rest of the function?
d) misunderstanding the codex, and it’s not as simple as just adding this to functions.php?

add_filter( 'registration_errors', 'myplugin_registration_errors', 10, 3 );
function myplugin_registration_errors( $errors, $sanitized_user_login, $user_email ) {

    if (!preg_match('( |^)[^ ]+@mydomain.co.uk( |$)', $user_email )) {
    $errors->add( 'invalid_email', __( 'ERROR: Only valid "mydomain" email address is allowed.' ));
    $user_email = '';
    }

    return $errors;
}

BTW, I’m aware that the regular expression I’ve used doesn’t fully test that a valid email address has been entered – all I need it to do is check that the domain is exactly right (and obviously this isn’t the ‘real’ one!!)

I also tried the solution in a previous post (2011) but it simply broke my whole site 🙁

I’ve been staring at this so long now, it could be blindingly obvious and I wouldn’t see it…

Many thanks!

--------------------------------------------------

>>> Share a link to this question, just copy and paste the code bellow on your web page <<<

<a href="https://www.wptricks.com/question/restrict-user-registration-to-emails-on-a-single-domain/">

Restrict user registration to emails on a single domain</a>
0
, , , wwwolf 1 week 0 Answers 19 views 0

Leave an answer