I wrote a plugin that allows people to Join A Multisite on a Per-Site Basis.
There are some things it doesn’t do that I have no intention of adding into the plugin, but people often ask me how to do them. Personally, I think people should always know they’re on a network, and hiding this will only lead to complaints later one, but my way is not the only way.
That said. I am aware of things people try to do that my plugin won’t. All of these snippets should go in a file in mu-plugins
. I’d name it multisite-registration.php
personally. That way I know what it is right away.
Emails By Site
When you register for a network site, you always get emailed from the network. This means even if I go to halfelf.org
to reset my password, the email always comes from ipstenu.org
. To change the password reset emails to be from the one where you’ve actually pressed the reset link is pretty easy:
add_filter( 'retrieve_password_message', function ($message, $key) { return str_replace(get_site_url(1), get_site_url(), $message); }, 10, 2); add_filter( 'retrieve_password_title', function($title) { return "[" . wp_specialchars_decode(get_option('blogname'), ENT_QUOTES) . "] Password Reset"; });
But when we talk about the activation it’s a little messier. If you’re using my plugin, you can have users sign up on a specific site. You probably want to have new user activations come from that site and if you do, you need to do this:
add_filter( 'wpmu_signup_blog_notification_subject', function($subject) { return "[" . wp_specialchars_decode(get_option('blogname'), ENT_QUOTES) . "] Activate Your Account"; }); add_filter( 'wpmu_signup_blog_notification_subject', function($subject) { return "[" . wp_specialchars_decode(get_option('blogname'), ENT_QUOTES) . "] Activate Your Account"; });
That subject can, obviously, be changed.
Redirect Lost Password Pages
So you may have noticed that the lost password page on a network always points to the network and never the site you’re on.
That can actually be fixed by doing this:
add_filter( 'lostpassword_url', function ($url, $redirect) { $args = array( 'action' => 'lostpassword' ); if ( !empty($redirect) ) $args['redirect_to'] = $redirect; return add_query_arg( $args, site_url('wp-login.php') ); }, 10, 2); add_filter( 'network_site_url', function($url, $path, $scheme) { if (stripos($url, "action=lostpassword") !== false) return site_url('wp-login.php?action=lostpassword', $scheme); if (stripos($url, "action=resetpass") !== false) return site_url('wp-login.php?action=resetpass', $scheme); return $url; }, 10, 3 );
This simply filters the URL and if you’re on a site’s login page, use that site for the URL.
Redirect Logins to Their Site
This one is messier. If you always want a user to be redirected to ‘their’ site, you have to know what their primary blog is on the network. You can do this, and for the most part, this works:
add_filter('login_redirect', function ( $redirect_to, $request_redirect_to, $user ) { if ($user->ID != 0) { $user_info = get_userdata($user->ID); if ($user_info->primary_blog) { $primary_url = get_blogaddress_by_id($user_info->primary_blog) . 'wp-admin/'; if ($primary_url) { wp_redirect($primary_url); die(); } } } return $redirect_to; }, 100, 3);
If they don’t have a primary_blog, they’ll be punted to the main for the network, as it should be.