How to redirect user to his primary blog on login in a multisite

Some web admins don’t like to to redirect the user to network site dashboard upon login. So, they want to redirect the user to the primary blog’s dashboard. Well, it’s fair enough.

So, if an user 5 subsites, and when he will login, he will be redirected to his primary blog’s dashboard. He will still be able to access all of his sites from My Sites link at the top left of admin bar. He is also able to change his primary blog.

So the following script will do the job:
[php]
function go_to_primary_blog($redirect_to, $request, $user){
$user_info = get_userdata($user->ID);
return get_blogaddress_by_id($user_info->primary_blog) . ‘wp-admin’;
}
add_filter(“login_redirect”, “go_to_primary_blog”, 10, 3);
[/php]

You can add those codes in your functions.php in the theme, if you think your theme won’t be changed. Otherwise mu-plugins is the best solution. To use mu-plugins, go to /wp-content/ and find the folder with name ‘mu-plugins’. If there is no folder in that name, then create a folder, name it ‘mu-plugins’, create a file inside that, give any name you like and paste the code in there. You don’t need to activate that plugin. Mu-plugins means must use plugins, so it will be activated automatically always. If you use mu-plugins then add a php start tag <?php at the beginning of the code.

Hope it helps!

Leave a Reply

%d bloggers like this: