您可以选择不允许用户在主站点注册,只允许用户在子网站注册。插件network-subsite-user-registration 允许这样做,如果用户已经在网络上注册,则插件会自动将其添加到请求注册的站点。执行此操作的挂钩是“wpmu\\u validate\\u user\\u 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;
}