Log in to the main network site from a subdomain

Hi,

I am trying to login programmatically from a subdomain of the type: app.mysite.com and I want to log in the user into the main network site and the site he own.

I am using the following code with no success:

wp_clear_auth_cookie();
wp_set_current_user( $user_id );
wp_set_auth_cookie( $user_id );

I just want to mimic the default login form action when logged in from the main site, but the code above only log the user in to the current subdomain the code is executed on, and I need the user to be logged in as if he used the mysite.com/login instead of app.mysite.com/login.

So basically, I want the user to be logged into the main site and the sites he owns, programmatically from a script placed on app.mysite.com.

I hope it is clear.

Regards.

  • James Morris
    • WordPress Enthusiast

    Hello Andreu,

    I hope you are well today. Thank you for your question.

    I see you have the Domain Mapping plugin installed on your sites. Have you tried using the Cross-domain autologin feature of Domain Mapping? You can enable this in Network Admin -> Settings -> Domain Mapping -> Mapping Options.

    As you can see by the following screenshot, my test user can have access to all of his websites by signing in one time. :slight_smile:

    Let me know if you have any further questions. I'll be happy to help! :slight_smile:

    Best regards,

    James Morris

  • Andreu
    • Design Lord, Child of Thor

    Hi,

    Thank you for your quick reply.

    Yes I have that option enabled, but it doesn't affect.

    I will try to simplify my question because it seems too chaotic: Can you login into app.example.com and have login access to example.com, automatically?

    I hope it is more clear now.

    Regards

  • James Morris
    • WordPress Enthusiast

    Hello Andreu,

    Provided both sites are on the same multisite network, and provided the account you are logging in with is present on both subsites, yes, it should work.

    When I tested this on a fresh install of WordPress multisite with Domain Mapping enabled, it worked correctly. If it is not working correctly for you, please check to make sure the account you are logging with is present on both subsites.

    Let me know if you have any further questions. I'll be happy to help! :slight_smile:

    Best regards,

    James Morris

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.