Redirect users to their blogs homepage

Hi, this is really a continuation of the same question posed a couple of months ago, but is now closed.
https://premium.wpmudev.org/forums/topic/how-to-make-login-redirect-take-users-to-their-blogs-homepage

In the last thread, David came up with a solution:
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 ) ;

I tried adding this to my functions.php but it had no effect. Questions:
1. Did it need to be in a specific place in the functions.php file?
2. Does it need another closing tag after the last semi-colon?
3. Is the primary blog established when a user signs up for a new site? Or do I need to change sign-up to establish this?
4. My site owners are set-up as editors (should that matter) on their sites and subscribers on the main site?
5. When this works will it take them to their front-end or back-end dashboard?

  • DavidM
    • DEV MAN’s Mascot

    Hi antKat,

    There's been at least one deprecated function in that code since that point, so I'm working on a quick update for that in the form of a mu-plugin. I should be able to get that posted back here in just a moment.

    Thanks for your patience!

    -David

  • Mason
    • DEV MAN’s Sidekick

    Hiya antKat,

    I haven't used that code myself, but I'll ask David if he's still using it and can verify that it's still working.

    To answer a couple of your questions though:

    1. Did it need to be in a specific place in the functions.php file?

    No. Doesn't matter.

    2. Does it need another closing tag after the last semi-colon?

    Nope. No additional closing bracket.

    3. Is the primary blog established when a user signs up for a new site? Or do I need to change sign-up to establish this?

    Yes. You can assign a default blog for all users to be assigned to in the Network Settings, and even dictate which blogs a user becomes attached to with this plugin:
    http://wordpress.org/extend/plugins/wpmu-default-user-role/
    If a user signs up for a new site and they haven't been assigned to any other blog, their site should be their main blog.

    4. My site owners are set-up as editors (should that matter) on their sites and subscribers on the main site?

    When you go to Network Admin>Users which site is listed first in the list?

    5. When this works will it take them to their front-end or back-end dashboard?

    I believe it's the front end, but David can confirm that one way or the other.

    thanks!

  • DavidM
    • DEV MAN’s Mascot

    Thanks for clarifying that Mason, I was throwing together a quick mu-plugin for this but it'll take a bit more work to get it updated as get_usermeta is being deprecated. I'll see about getting that posted soon.

    @antKat, to help clarify, the functions.php from your primary site's theme would be the place to put the code and it should be fine anywhere within that as Mason mentioned.

    Regarding your last query, you can easily change where it directs to by changing line 6, similar to the following:
    return $redirect_url . "/wp-admin";

    I think the issue you're experiencing may have to do with the users not having a specified primary_blog. Could you take a look at that in your Network Admin > Sites listing as Mason was saying?

    Thanks,
    David

  • antKat
    • HummingBird

    HI Guys, Thanks for the quick replies.

    Because I installed Membership, some of the standard Wordpress functionality such as "Gimme a Site" on sign-up is lost; therefore, with Membership activated the user really only initially gets registered with our corporate (main site) rather than getting their own site at sign-up. Mason, you may recall trying to help me with this. Unfortunately, the links into the hooks is above my technical skill. It looked simple enough for someone who knows what they are doing. So, if I can't find someone to help me with the ability for our users to sign-up for a blog during the registration process, I will probably abandon Membership. I had really installed it as a structure for the future. If I delete Membership, the sites our user's get on sign-up should be their primary one. In either case, having the redirect to the user's front end is a preference I am trying to accomplish - so Dave, I appreciate you updating this.
    Regards.

  • Memiah
    • Design Lord, Child of Thor

    HI guys, I'm looking for a way to bypass the "You attempted to access the (main network site) dashboard, but you do not currently have privileges on this site. If you believe you should be able to access the "Memiah Sites" dashboard, please contact your network administrator."

    when my users sign up, the email contains a link to the main networks sites wp-login, not theirs, so they are alwats greeted with this message.

  • Mason
    • DEV MAN’s Sidekick

    Hiya MemiahMatt,

    Just drop this in your "mu-plugins" folder, in its own "my_login_redirect.php" file:

    <?php
    function ds_login_redirect( $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;
    }
    add_filter('login_redirect','ds_login_redirect', 100, 3);
    ?>

    There is no other activation necessary as any code in the "mu-plugins" is always on.

    The effect is that no matter what blog a user logs in at, member or not, s/he redirects to their primary.

    Thanks to dsader for this code snippet :slight_smile:

  • jdkint
    • New Recruit

    Hi guys,

    The code:

    <?php
    function ds_login_redirect( $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;
    }
    add_filter('login_redirect','ds_login_redirect', 100, 3);
    ?>

    wont work at my wordpress installation. When multsite user logins at "domain.com/wp-admin/", instead of "domain.com/user/wp-admin/", I still see "You attempted to access..", etc. Can you help please? Is it possible conflict with other plugins?

    Best,

    Jordan

  • Timothy Bowers
    • Chief Pigeon

    Hey Michael.

    You'll want to check for the main blog using is_main_site($blog_id) and then redirect when it is using wp_redirect()

    Here are the links for you to read on those:

    http://codex.wordpress.org/Function_Reference/is_main_site
    http://codex.wordpress.org/Function_Reference/wp_redirect

    Would you need to make some exceptions for say the network admin, if so you'll want is_super_admin()

    http://codex.wordpress.org/Function_Reference/is_super_admin

    Let us know if you how get on, and if you need any further help.

    Best to open a new thread for that though, this one is rather old and everyone on here will most likely get notified of every response we make. :slight_smile:

    You can open a new thread here:

    https://premium.wpmudev.org/qanda

    You can of course reference this one if you feel it would help :slight_smile:

    Have a great day!

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.