0

I need help modifying the code below. I'm using the Ultimate Member plugin on my Wordpress site for membership. I only want those people within my organization to be able to register for the site (my departments use different domains for their email addresses, it's a headache and I don't want to get into that). Right now, it will automatically validate emails from @company1.com, but I need to add up to 10 more email addresses to that code to perform automatic validation. Basically, anyone that doesn't have an email address listed, is automatically denied membership to the site.

add_action('um_before_new_user_register', 'require_google_email_for_signup');
function require_google_email_for_signup( $args ) {
    extract($args);
    if ( !strstr( $user_email, '@company1.com' ) )
        exit( wp_redirect( add_query_arg('err', 'you_must_have_googlemail') ) );
}

4 Answers4

1

"um_before_new_user_register" has been removed from the 2.x version. You can use the below working code for complete form validation not just a single field.

add_action('um_submit_form_errors_hook_','um_custom_validate_form', 999, 1);
function um_custom_validate_form( $args ) {
    global $ultimatemember;

    $user_email = $args['user_email'];

    if ( !strstr( $user_email, '@domain1.com' )){
        $ultimatemember->classes['form']->add_error( 'user_email', 'You must register with a valid email address.' );
    }
}
Ankit Chauhan
  • 384
  • 2
  • 18
0

Use this code:

<?php
add_action('um_before_new_user_register', 'require_google_email_for_signup');
function require_google_email_for_signup( $args ) {
    extract($args);

    /* add multiple domains name here */
    $allow_domains = ['company1.com', 'company2.com', 'company3.com'];

    /* get domain name from user email */
    $domain_name = substr(strrchr($user_email, "@"), 1);

    if (!in_array($domain_name, $allow_domains)){
        exit( wp_redirect( add_query_arg('err', 'you_must_have_googlemail') ) );
    }
}
?>
Chandra Kumar
  • 4,127
  • 1
  • 17
  • 25
  • I changed the @company1.com email addresses to my company's actual email domains but that code didn't work. Every single email domain I tried would not allow me to register. – Eric Nicastro Aug 01 '17 at 20:11
0

strstr() is more memory intensive compared to strpos(), so I will recommend using the latter. When dealing with an array, you can use the following iterative logic:

  1. Set a variable, say $check, to false
  2. Iterate through each allowed domain in a for loop
  3. Whenever a match is found (by using strpos()), we set $check to true and break out of the loop. This ensures that we do not traverse the entire array when a match is already found
  4. Evaluate $check, and decide whether to throw an error / exit

Hint: I would recommend converting your user-email to lowercase when using strpos (or you can use stripos()), because some users may enter emails that are of mixed cases.

With that in mind, here is an example:

<?php
add_action('um_before_new_user_register', 'require_google_email_for_signup');
function require_google_email_for_signup( $args ) {

    extract($args);

    // Store allowed domains in an array
    $allowed_domains = ['@company1.com', '@company2.com', '@company3.com'];

    // Set flag to false (fail-safe)
    $check = false;

    // Iterate through all allowed domains
    foreach( $allowed_domains as $domain ) {

        // If a match is found (remember to use lowercase emails!)
        // Update flag and break out of for loop
        if ( strpos( strtolower( $user_email ), $domain ) !== false ) {
            $check = true;
            break;
        }
    }

    if ( !$check )
        exit( wp_redirect( add_query_arg('err', 'you_must_have_googlemail') ) );
}
Terry
  • 63,248
  • 15
  • 96
  • 118
0

This custom code no longer works in the latest version 2 update to ultimate member. The hook was removed, so it’s no longer possible to do email address blocking with this code. Does anyone have any suggestions on how to get this to work again? Here is the code I'm using:

/* ULTIMATE MEMBER PLUGIN DOMAIN WHITELISTING CODE SNIPPET
enter code here`The following code will require a domain name to be  
whitelisted for user `enter code here`registrations.     
It forces a user email to match one included in this code at registration.
You can add any provider you want by copying and pasting a new line as per 
instructions.*/

add_action('um_before_new_user_register', 'force_google_email_for_avnw_signup');
    function force_google_email_for_avnw_signup( $args ) {
       extract($args);
       if ( !strstr( $user_email, '@anydomain.com' ) )

          exit( wp_redirect( add_query_arg('err', 'whitelisted_email_required') ) );
    }