1

When new site is created on my multisite installation, I automatically add the new user to the main site (because I need him to order the product):

add_user_to_blog( 1, $user_id, 'customer' );

I'd like to have the users automatically login them to all their sites, as soon as they login to one of the sites they belong to.

I hoped to hook to the wp_login action and just set the auth_cookie for each sites the user belongs to, something along these lines:

add_action('wp_login','autologin');
function autologin($user_login, $user) {
   $user_blogs = get_blogs_of_user($user->ID);
   foreach ($blogs as $blog) {
     switch_to_blog($blog->blog_id);
     wp_set_current_user ( $user->ID );
     wp_set_auth_cookie  ( $user->ID);
     restore_current_blog();
   }
}

That doesn't work though. Is there a workaround for this?

3
  • 1
    You dont state if using WP subdomain or subdirectory. In addition its possible to configure WP multisite to host completely different domains, e.g. domain1.com, domain2.com. You cannot log a user into different domains. WP uses cookies and your browser will reject setting cookie for a different domain. A different approach will work for subdomains, *.domain.com.
    – user42826
    Commented Oct 3, 2016 at 19:57
  • Subdomain install, I have already define('COOKIE_DOMAIN',false); set to allow displaying multisite blogs on different domains. Commented Oct 3, 2016 at 20:50
  • Like I mentioned if you have different domains, the browser will block setting cookies other than your current site. Subdomains are different, my WP site has over 400 site, I only have to log into one site and that cookie is good for all (subdomain) sites.
    – user42826
    Commented Oct 3, 2016 at 21:37

1 Answer 1

0

You can add each user automatically to each blog in your network. As example the follow simple function to add a user id to a blog id. If you loop about all sites and add the user to each blog with a role.

function join_site( $user_id = 0, $blog_id = 0, $role = 'editor' ) {

    if ( 0 === $user_id )
        return false;

    if ( 0 === $blog_id )
        return false;

    if ( ! is_user_logged_in() )
        return false;

    if ( ! is_user_member_of_blog( $user_id, $blog_id ) )
        add_user_to_blog( $blog_id, $current_user->ID, $role );
}

You can run this function on the register of a user or also on the login. On the register is smaller in the performance, only one request after register, not each login. But the function checks, if the user a part of the plugin, with the help of is_user_member_of_blog().

The benefit is, that each user is a real part of the blog, maybe with different roles. The result is, that you have all possibilities, also for other plugin to run functions the users.

For the login you should set the constant to define always the same cookie.

define( 'COOKIE_DOMAIN', '' );

That give the users the possibility to login in each site of the network, if the user is added to the site. For more in this context see this q/a.

4
  • Please read carefully my question - I explicitly stated that I already added the user to the blog. What I need is to login the user automatically to all his site once he logs in one of his sites. Commented Oct 3, 2016 at 13:13
  • 1
    You should read this answer to the login topic in a Multisite, should helps you.
    – bueltge
    Commented Oct 3, 2016 at 15:31
  • The answer says that this is not possible to do as far as I can say...:( Commented Oct 8, 2016 at 12:35
  • Yes, you can use the snippet to add each user to each site and add a single sign on. The cookie makes easier to logged in. Maybe a sso login, plugin helps also to solve your goal.
    – bueltge
    Commented Oct 8, 2016 at 18:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.