0

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!

  • Try logging the email address to see if it's escaped in some way that makes the expression fail. – Andy Macaulay-Brook Aug 9 '16 at 21:07
0

You need to use delimiters in order for your regex to work.

From the link above

When using the PCRE functions, it is required that the pattern is enclosed by delimiters. A delimiter can be any non-alphanumeric, non-backslash, non-whitespace character.

so in your case, you could have something like this. Notice how I used the / to delimit the regex

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;
}

I notice you allow white space at the beginning and the end of your regex.

Maybe you want to use this regex to avoid allowing whitespace (unless this is the desired outcome)

if (! preg_match('/^[a-zA-Z0-9._-]+@mydomain\.co\.uk$/', $user_email )) {...

  • ^ means starting from
  • [a-zA-Z0-9._-] means any alphanumerical caracter and dot . underscore _ and dash - characters are allowed
  • + means one or more of the preceding token, [] in this case
  • \. escapes the dot so it's not interpreted as any characters
  • $ means ending here

also note that since you filter into WP registration validation mechanism, you are adding to the current check, so technically, WP will check if this is a valid email on top of what you are asking

  • 1
    Thank you!! Works a treat :) I knew it was going to be some simple syntax error, but could I find it?! Of course, the client has now decided they don't want that feature after all, but hell, it was fun, I can offer it to others, and I learned something that will definitely be useful down the line. – wwwolf Aug 11 '16 at 8:16
  • It is an handy feature, too bad the client doesn't want it anymore. You could accept this answer so it's marked as solved. cheers ;) – bynicolas Aug 14 '16 at 16:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.