• ccgauvin94

    (@ccgauvin94)


    Hi. I’m trying to write a simple plugin to redirect a user to their home site on a multisite if they log in on a multisite. So basically if they go to mymultisite.com and login, I want them to be redirected to the multisite they registered to (their source domain) so like mysite.mymultisite.com.

    This is what I have:

    <?php
    
    add_filter( 'login_redirect', 'send_subscribers_home', 10, 3 );
    
    function send_subscribers_home( $location, $request, $user ) {
        global $user;
        if ( isset( $user->roles ) && is_array( $user->roles ) ) {
            if ( in_array( 'customer', $user->roles ) ) {
    			$user_ID = get_current_user_id();
    			$homeRedirect = get_user_meta($user_ID, 'source_domain');
    			$homeRedirectFinal = json_encode($homeRedirect);
                return $homeRedirectFinal;
            } else {
                return $redirect_to;
                }
        }
        return;
    }
    
    ?>

    So basically I’m intercepting the login_redirect filter, checking if the user is in a certain group, and if so, grabbing the website they originally registered to (in this case it will be a subdomain) and redirecting that.

    I’ve never written a WordPress plugin before. Anyways the problem is that this returns “www.mymultisite.com/www.mysite.mymultisite.com” as a web page, and obviously throws a 404. Does anyone know if I can change how WordPress parses this address (so it’s not as a page of the main site, but rather just an external URL) without modifying wp-login.php ?

    Thanks.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    Why would you want to change what WP does with the address? Doing so would likely foul up the multisite functionality in all sorts of ways.

    Why don’t you simply extract the data you need out what WP returns and build your own proper destination URL? PHP’s preg_match() should do that easily. Or maybe even easier is to explode() what’s returned on the dots and build your URL from the last 3 elements.

    Thread Starter ccgauvin94

    (@ccgauvin94)

    I’m not sure entirely what you mean.

    Basically I just want to redirect after login to something other than the admin panel. It seems like the best way to do this is to use the filter that’s already in place. Problem is, that only appends the page to the site’s home URL. I want to redirect to an entirely different multisite altogether.

    I’m not positive how preg_match() would help me. Doesn’t that just check for a string and return a logic value?

    Moderator bcworkz

    (@bcworkz)

    preg_match() does indeed check a string and return a boolean. But it can do much more when you provide a third $matches argument. $matches will contain any regexp matches that where captured by using parenthesis in the regexp .

    preg_match('!.*\/www\.(.*)!', 'www.mymultisite.com/www.mysite.mymultisite.com', $matches);
    header("Location: https://{$matches[1]}/");
    exit();

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Redirect to External URL’ is closed to new replies.