How to make login-redirect take users to their blogs homepage

How do I make login-redirect take users to their blogs homepage instead of the main site when they login. I tried the plugins I found in the forum but none seem to do what I need. I even tried Peter's Redirect and it has a variable which is nice but didn't work with the network as far as I can tell.

Thanks in advance.

  • DavidM
    • DEV MAN’s Mascot

    Hi wecreateyou,

    Unfortunately, that wouldn't be possible with the Login Redirect plugin itself as it requests a specific url for the redirect. And in fact, I haven't yet seen a way to achieve exactly that, so I'm hoping someone else around here might have some good ideas on this.

    I was thinking that this could be done using the switch_to_blog() function, cycling through all the available blogs and checking if the logged in user is admin, but I don't have anything concrete on this as yet, just some jumbled code. I'll keep working at it and let you know if I'm able to hack together something that would work, though you might want to consider posting a job request if you need this right away.
    https://premium.wpmudev.org/wpmu-jobs/

    Cheers,
    David

  • DavidM
    • DEV MAN’s Mascot

    Hmm, come to think about it, if you set the redirect url to yoursite.com/wp-admin/, it should redirect the users to their own sub-site dashboard as long as they don't have a role on yoursite.com. If you redirect them to yoursite.com/wp-admin/my-sites.php, if they're not automatically taken to their site dashboard they'll at least have the option right before them to be taken there.

  • DavidM
    • DEV MAN’s Mascot

    Woohoo! Just got it working on a test multisite after a bit too much work! You wouldn't need switch_to_blog() at all. You can simply pop the following in your theme's functions.php and it should work from there!

    function login_site_redirect ( $redirect_to ) {
    	global $user;
    	$primary_blog_id = get_usermeta($user->ID, 'primary_blog');
    	$blog_details = get_blog_details($primary_blog_id);
    	$redirect_url = $blog_details->siteurl;
    	return $redirect_url;
    }
    
    add_filter ( 'login_redirect', 'login_site_redirect', 10, 3 ) ;

    Had another member request something similar. Let me know if you have any trouble with it!

    Cheers,
    David

  • aecnu
    • WP Unicorn

    Greetings David :slight_smile:

    I cannot seem to get this to work.

    I copied the code above into the functions.php file of the theme of the main site and it did not work. Actually nothing changed that I am aware of.

    Does it require the login redirect plugin to be activated?

    or something more then just popping it into the functions.php?

    Joe :slight_smile:

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.