0

I'm working on setting up a multisite environment, and have run into the possibility of a user trying to register on the sub-site with the same email address they've used on the main site. I've found some code here which removes the duplicate email address error and accomplishes this task.

Here is the relevant code from the answer linked above:

add_filter('wpmu_validate_user_signup', 'skip_email_exist');
function skip_email_exist($result){
    if(isset($result['errors']->errors['user_email']) && ($key = array_search(__('Sorry, that email address is already used!'), $result['errors']->errors['user_email'])) !== false) {
        unset($result['errors']->errors['user_email'][$key]);
        if (empty($result['errors']->errors['user_email'])) unset($result['errors']->errors['user_email']);
    }
    define( 'WP_IMPORTING', 'SKIP_EMAIL_EXIST' );
    return $result;
}

The difference in my case, is that we're only allowing users to register through a WooCommerce purchase on the sub-site and, evidently, WooCommerce doesn't use the same hooks to validate the email address.

Is there an action I can hook into so that I can perform the same functionality through WooCommerce?

1 Answer 1

0

There is the option that you don't allow the user to register on the main site, allowing them to register on the subsite only. The plugin network-subsite-user-registration allows this, if the user is already registered on the network then they are automatically added, by the plugin, to the site requesting registration. The hook that does this is 'wpmu_validate_user_signup'..

// check and add users if already on the network 
add_filter( 'wpmu_validate_user_signup', 'add_exting_user' );

/**
 * If users are already registered with the Network then simply add them
 * to this new site. 
 *
 * @return $result or drops out
 */
function add_exting_user( $result ) {

    if ( is_user_logged_in() ) {
        return $result;
    }

    $submitted_user_email = $result['user_email'];
    $original_error = $result['errors'];  

      foreach( $original_error->get_error_codes() as $code ){
         foreach(  $original_error->get_error_messages( $code ) as $message ){  
               if( $code != 'user_email' && $message == __( 'Sorry, that username already exists!') ){                    
                    $user = get_user_by( 'email', $submitted_user_email );
                    $user_id = $user->ID;
                    $blog_id = get_current_blog_id();
                    add_user_to_blog( $blog_id, $user_id, get_site_option( 'default_user_role', 'subscriber' ) );
                    $user_blogs = get_blogs_of_user( $user_id );

                    $user_blogs_sorted = array();
                    foreach ( $user_blogs AS $user_blog ) {
                            $user_blogs_sorted[ $user_blog->blogname ] = $user_blog->siteurl;
                    }

                    // A real quick way to do a case-insensitive sort of an array keyed by strings: 
                    uksort($user_blogs_sorted , "strnatcasecmp");

                    $html = "<h1>";
                    $html .= sprintf(  __('Hi %1$s you have been added to this site, your current sites on the Network are:', 'network-subsite-user-registration' ), "<strong>$submitted_user_email</Strong>" );
                    $html .= "</h1></Br><ul>";
                    foreach ( $user_blogs_sorted AS $sitename => $siteurl ) {
                        if ( ! is_main_site( $user_blog->userblog_id ) ) {
                                    $html .=  '<li><h2><strong><a href="' . wp_login_url($siteurl )   . '" target="_blank" >' . $sitename  . '</a></strong></h2></li>';
                            }
                    }
                    $html .= "</ul>";    

                    die( $html );

               }
         }
    }   

    return $result;  

}
2
  • Just for clarification, does this plugin only allow users to register on subsites? For my purposes, that won't work because we still have to allow users to register on the main site. Please let me know, though!
    – mcon
    Commented Mar 22, 2017 at 13:27
  • The plugin allows for any combination so main site only, one or more subsite only or main and any number of subsites to have user registration. So if you wanted a user to have the main site and one subsite then with the plugin they would need to register on both, the difference with the second time they register is that they are already known on the network so they just get given the second site 'subscriber' access with no need to activate the user account since this is ready completed.
    – Justin
    Commented Mar 24, 2017 at 9:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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