Useful Snippets

Welcome!


This blog is used to collect useful snippets related to Linux, PHP, MySQL and more. Feel free to post comments with improvements or questions!

Are your smart devices spying on you? Make better purchasing choices and find products that respect your privacy at Unwanted.cloud

RSS Latest posts from my personal blog


Most viewed posts


Subscribe to RSS feed


Redirect WordPress users to their primary site when logging in on a multisite

Stanislav KhromovStanislav Khromov

Here’s a quick plugin from this thread. Create this as the file redirect-users.php in /wp-content/mu-plugins/

From here on your users will be redirected to their primary site upon logging in, and you will never see that nagging “You attempted to access the (main network site) dashboard, but you do not currently have privileges on this site.” error!

<?php
/*
Plugin Name: Redirect Users to Primary Site
Plugin URI:
Description: Never see "you do not currently have privileges on this site" when logging in on your multisite ever again!
Version: 2014.06.02
Author: khromov
Author URI: https://profiles.wordpress.org/khromov
License: GPL2
*/

/* http://premium.wpmudev.org/forums/topic/redirect-users-to-their-blogs-homepage */
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) . 'wp-admin/';
            if ($primary_url) {
                wp_redirect($primary_url);
                die();
            }
        }
    }
    return $redirect_to;
}, 100, 3);

The only shortcoming with this plugin is that it will redirect admins back to their primary site (blog id = 1) if they sign onto another site. To fix this we need a slightly more complicated solutions, which checks if the user has access to the blog they are logging in to and only if they do not have access do you redirect. If you prefer this behaviour you can use this version of the plugin:

<?php
/*
Plugin Name: Redirect Users to Primary Site
Plugin URI:
Description: Never see "you do not currently have privileges on this site" when logging in on your multisite ever again!
Version: 2014.06.02
Author: khromov
Author URI: https://profiles.wordpress.org/khromov
License: GPL2
*/

/* http://premium.wpmudev.org/forums/topic/redirect-users-to-their-blogs-homepage */
add_filter('login_redirect', function($redirect_to, $request_redirect_to, $user)
{
    global $blog_id;
    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) . 'wp-admin/';
            $user_blogs = get_blogs_of_user($user->ID);

            //Loop and see if user has access
            $allowed = false;
            foreach($user_blogs as $user_blog)
            {
                if($user_blog->userblog_id == $blog_id)
                {
                    $allowed = true;
                    break;
                }
            }

            //Let users login to others blog IF we can get their primary blog URL and they are not allowed on this blog
            if ($primary_url && !$allowed)
            {
                wp_redirect($primary_url);
                die();
            }
        }
    }
    return $redirect_to;
}, 100, 3);

Web Developer at Aftonbladet (Schibsted Media Group)
Any opinions on this blog are my own and do not reflect the views of my employer.
LinkedIn
Twitter
WordPress.org Profile
Visit my other blog

Comments 10
  • Abhimanyu Singhal
    Posted on

    Abhimanyu Singhal Abhimanyu Singhal

    Reply Author

    Hey, I have been using this snippet as an mu-plugin successfully on my multisite for an year or so.

    But, recently there was a permissions issue with the server (hostgator) disabling change in userinfo for all accounts. I got it fixed by them but that has rendered this plugin to be not working on sites registered after the permissions reset.

    Please help.


    • Stanislav Khromov
      Posted on

      Stanislav Khromov Stanislav Khromov

      Reply Author

      Hey Abhimanyu,

      I have never heard of a web host disabling change in userinfo… Sounds crazy. I would suggest you properly set the users primary_blog. It’s in usermeta and can be set like this:

      update_user_meta( get_current_user_id(), 'primary_blog', $PRIMARY_BLOG_ID);
      

      You could loop over all users, check if they have a primary blog, and if they do not, grab their blogs via get_blogs_of_user() and set the first blog as primary_blog. Should resolve the issue.


      • Abhimanyu Singhal
        Posted on

        Abhimanyu Singhal Abhimanyu Singhal

        Reply Author

        Okay, I guess it was an issue with some permissions. And, about this code, could be elaborate a little more on where to add it and stuff? ;)

        Sorry, I’m quite a dumbass when it comes to php.

        Thanks for your time, Stan.


        • Abhimanyu Singhal
          Posted on

          Abhimanyu Singhal Abhimanyu Singhal

          Reply Author

          I mean could you tell me where to place this code?


        • Stanislav Khromov
          Posted on

          Stanislav Khromov Stanislav Khromov

          Reply Author

          Hey Abhimanyu,

          You would need to first find out why the original code is not working. The issue may lie somewhere else than faulty usermeta, for example some other plugin may hook into login_redirect and overwrite the logic. So I would really advise you to find someone knowledgeable in WordPress custom coding for this task! :)


  • Oran
    Posted on

    Oran Oran

    Reply Author

    You ARE THE MAN ! I look for this solution everywhere and I only find it here.
    I’m searching for couple days a solution for doing the same primary_url redirection to Facebook logins but I can’t find it anywhere !! :/ I thought to ask maybe you wrote something about that as well ?


  • Oran
    Posted on

    Oran Oran

    Reply Author

    Hey thanks for the quick respond ! I actually figure it out the facebook issue.

    However, I’m currently using Networks for WordPress plugin.
    Which I believe not keeping the users logged in after they redirected to their primary_url.

    They redirect to the primary_url which is perfect because of your plugin. but they not stay logged. They have to log-in again. Perhaps maybe a master like you will know how to keep them logged after they redirected ?

    I also ask for help in their support forum and linked to your blog.
    https://wordpress.org/support/topic/keep-user-logged-into-my-networks-after-they-login-through-one-network?replies=1#post-6052127

    Thanks again for this plugin !


  • Oran
    Posted on

    Oran Oran

    Reply Author

    okey I found the solution i’m looking for. It was actually related to the cookies.
    adding this the my config.php will do the job.
    // Cookies
    define( ‘COOKIEHASH’, md5( ‘jpoi.org’ ) );
    define( ‘COOKIE_DOMAIN’, ‘jpoi.org’ );
    define( ‘ADMIN_COOKIE_PATH’, ‘/’ );
    define( ‘COOKIEPATH’, ‘/’ );
    define( ‘SITECOOKIEPATH’, ‘/’ );
    define( ‘TEST_COOKIE’, ‘thing_test_cookie’ );
    define( ‘AUTH_COOKIE’, ‘thing_’ . COOKIEHASH );
    define( ‘USER_COOKIE’, ‘thing_user_’ . COOKIEHASH );
    define( ‘PASS_COOKIE’, ‘thing_pass_’ . COOKIEHASH );
    define( ‘SECURE_AUTH_COOKIE’, ‘thing_sec_’ . COOKIEHASH );
    define( ‘LOGGED_IN_COOKIE’, ‘thing_logged_in’ . COOKIEHASH );


  • FM
    Posted on

    FM FM

    Reply Author

    Hi Stanislav

    Thanks for this snippet!

    Together with a LOGIN redirect, as the snippet above does, I also want to redirect users if they visit any site that is not their primary site.

    So, a logged-in user can only visit his own subsite. If they visit any page on any site that is not their own subsite, they are redirected to their own subsite.

    Is that possible?