Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WordPress Login redirect_to not working

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);
like image 746
Adam Erstelle Avatar asked Dec 03 '13 16:12

Adam Erstelle


People also ask

Why is WordPress login not working?

Common reasons why you can't access wp-adminYou're being blocked by your security plugin. You changed the WordPress login URL. Your WordPress memory limit is too low. There's a problem with your WordPress site (White Screen of Death, 500 Internal Server Error, etc.)

How do I find my WordPress admin URL?

The simplest way to find your WordPress login URL is to add /admin to the end of your site URL. For example, if your WordPress site is www.mywebsite.com , you can access your login page by visiting www.mywebsite.com/admin .


2 Answers

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.

like image 155
Bilal Avatar answered Oct 10 '22 23:10

Bilal


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'];
    }
}
like image 26
RafaSashi Avatar answered Oct 11 '22 01:10

RafaSashi