Redirect User to their home page

I have read the forums regarding the redirect subject matter. I've used the code in may of the redirect forum topics but I am still not getting the results. I tried this code. I placed it in my sites function.php

add_filter('login_redirect', function($redirect_to, $request_redirect_to, $user)
{
if (!is_wp_error($user) && $user->ID != 0)
{
$user_info = get_userdata($user->ID);
if ($user_info->primary_blog)
{
$primary_url = get_blogaddress_by_id($user_info->primary_blog) . '';
if ($primary_url) {
wp_redirect($primary_url);
die();
}
}
}
return $redirect_to;
}, 100, 3);

the result is the user gets redirect to a protected page page.
https://360project.checkuptech.com/protected-content/?redirect_to=https%3A%2F%2F360project.checkuptech.com%3A443%2Fhome%2Fdemo-unified-school-district%2F

when I need the use to be directed to this page:
https://norwalkusd.checkuptech.com/

the login page is

any help with this will be greatly appreciated.

cheers

  • Luís

    Hi Mark ,

    Hope you're doing well today!

    In order to help you better, let me try understand better the process. I think the code shown above it's used to redirect the user to their primary blog after login in the main website.

    What should be achieved? Redirect all users to a specific page? (https://norwalkusd.checkuptech.com/)

    Redirect the users to their primary blog?

    I think you missed the login page, can you give us access to your admin panel so I can check your setup, in order to replicate it and try other solutions?

    If you don't already know, please, follow the link below, to see how enable the staff login:

    http://premium.wpmudev.org/manuals/wpmu-dev-dashboard-enabling-staff-login/

    When you enable this option, please post again!

    Cheers, Luís

  • Jude

    Hey Mark

    Hope you're having a great weekend !

    Here is some code to do this for you

    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) . '/' ;
                if ($primary_url) {
                    wp_redirect($primary_url);
                    die();
                }
            }
        }
        return $redirect_to;
    }
    add_filter('login_redirect','ds_login_redirect', 100, 3);

    Insert that as a mu-plugin. To create an mu-plugin, go to your /wp-content/ folder via FTP, and find the folder called "mu-plugins". If there is no folder with that name, then you'll want to create one. Then, create a file inside that folder, and give the file any name you like (making sure the file as an extension of .php, e.g. example.php) and paste the code in there.

    You don't need to activate that plugin, as it will be always be automatically activated. Please be sure to add a PHP start tag at the beginning of the code if you insert this code as a mu-plugin, like this:

    <?php

    Thank you for being a WPMU DEV member and have a fantastic day!

    Cheers
    Jude

  • Adam Czajczyk

    Hello Mark!

    I watched your screencast and I got slightly confused but let's tray to get it sorted :slight_smile:

    I understand that the screencast was recorded with Jude's code applied to the site and it is actually redirecting user to their respective sub-site, is that right?

    The issues I think I see there are:

    1. After login user is redirected to the homepage of the sub-site instead of the "demo-unified-school-district" page

    2. Some users are taken to the "Protected Content" after login.

    Am I on a right path?

    As for #1.

    If that page is the same for each subsite, like:

    site1.mysite/demo-unified-school-district
    site2.mysite/demo-unified-school-district
    site3.mysite/demo-unified-school-district
    site4.mysite/demo-unified-school-district and so on...

    you could simply adjust Jude's code by replacing this line

    $primary_url = get_blogaddress_by_id($user_info->primary_blog) . '/' ;

    with this one

    $primary_url = get_blogaddress_by_id($user_info->primary_blog) . '/' . 'demo-unified-school-district/ ';

    As for #2.

    This doesn't mean that redirect doesn't work but only that the user that logged in doesn't have a right to access particular site so most likely membership issue has to be taken care of. You'd need to make sure that the user that's logging in actually is a member of a membership that's protecting the "target" page.

    Best regards,
    Adam

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.