9
votes

I'm working on a web site with WordPress 3.7.1 and trying to redirect a user upon login via the redirect_to URL parameter.

I've verified that the login form backend sees the redirect_to GET parameter, and that the login submission includes the redirect_to POST value...but thats where things don't work.

Removed link and user credentials

Upon login (for both my Admin acct and the Subscriber acct provided in this post), the user is taken to the WP dashboard instead of the URL in the redirect_to parameter.

I do have the allowed_redirect_hosts being set, with the following code in a custom plugin file (which is pretty much just this).

add_filter( 'allowed_redirect_hosts' , 'glue_allowed_redirect_hosts' , 10 );
function glue_allowed_redirect_hosts($content){
    $content[] = 'app.realestategradschool.com';
    $content[] = 'dev-app.realestategradschool.com';
    $content[] = 'app.realestategradschool.local';
    return $content;
}

I've disabled all other plugins in an attempt to troubleshoot this.

Edit: I can't use login_redirect as I'm not looking to redirect ALL logins...only if the visitor is sent to the login page from a different site (using oAuth to log them in...oAuth works...just not the redirect)

Edit: Working solution:

function glue_login_redirect($redirect_to,$request='',$user=null){
    //using $_REQUEST because when the login form is submitted the value is in the POST
    if(isset($_REQUEST['redirect_to'])){
        $redirect_to = $_REQUEST['redirect_to'];
    }
    return $redirect_to;
}
add_filter('login_redirect','glue_login_redirect',999);
3
The other thing I have in that plugin file is a declaration of wp_validate_redirect where I copied the code from WordPress CORE and simply added a single debug statement. My issue remains wether or not this code is left intact. - Adam Erstelle
Why not use login_redirect, did you use it already ? - The Alpha
Your login_redirect code did not work for me. - Andrew Truckle
It has been a few years since this was posted, it is possible that things might have changed since then. - Adam Erstelle

3 Answers

8
votes

You can use login_redirect filter. See here http://codex.wordpress.org/Plugin_API/Filter_Reference/login_redirect

I think this is what you are looking for.

This will normally redirect all logins. To be able to redirect only when you want, you can use a query string parameter in the URL. Check for the parameter, if it exists redirect.

1
votes

Try to add this in your template functions.php file:

add_action( 'login_form' , 'glue_login_redirect' );
function glue_login_redirect() {
    global $redirect_to;
    if (!isset($_GET['redirect_to'])) {
        $redirect_to = 'YOURURLHERE';
    }
    else{
        $redirect_to = $_GET['redirect_to'];
    }
}
0
votes

Maybe this code help you.

<input type="hidden" name="redirect_to" value="<?php echo $_SERVER['REQUEST_URI']; ?>" /> 

Its redirect users to where login from. example. if ya login from home page or post. Its login then redirect back to homepage or login page.

This code main part is :

<?php echo $_SERVER['REQUEST_URI']; ?>