Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a custom login page at http://netballscoop.com/log-in/

When you fill the username and password it redirects you to the previous page you were on perfectly. When you click Log Out it redirects you to the same page. So this all works well.

If you leave the username/password blank or have an error it will lead you to wp-login. To prevent this I have used the code from http://stackoverflow.com/questions/11477107/redirect-away-from-login-page

add_action('login_redirect', 'redirect_login', 10, 3);
function redirect_login($redirect_to, $url, $user) {
if($user->errors['empty_password']){
    wp_redirect(get_bloginfo('url').'/log-in-error/');
}
else if($user->errors['empty_username']){
    wp_redirect(get_bloginfo('url').'/log-in-error/');
}
else if($user->errors['invalid_username']){
    wp_redirect(get_bloginfo('url').'/log-in-error/');
}
else if($user->errors['incorrect_password']){
    wp_redirect(get_bloginfo('url').'/log-in-error/');
}
else{
    wp_redirect(get_bloginfo('url').'/log-in');
}
exit;
}

This works great for log in errors on my website. But now the login redirect takes you to http://netballscoop.com/members/testing (The user 'Testing' is taken tor their BuddyPress member's profile page).

How do I redirect the user to the previous page when they log in?

share|improve this question
    
My previously posted answer misread your question a little bit so I deleted it, have you tried tying into the wp_login action already? It is one of the last one's fired when logging in successfully. I used it on a site I recently built using BuddyPress and it worked like a charm. –  Eric Allen Oct 4 '12 at 4:39
    
Hi @allenericr, to be honest I would not know where to start with that. My knowledge of php/functions/etc is very limited. Would you be able to post what you are using for that site if you find it is a better alternative? –  Tanya Oct 4 '12 at 5:07
add comment

1 Answer

up vote 3 down vote accepted

You can filter the login_redirect function like so, adapt according to your needs.

add_filter('login_redirect', 'redirect_previous_page', 10, 1);

function redirect_previous_page( $redirect_to ){
    global $user;

    $request = $_SERVER["HTTP_REFERER"];

    if ( in_array( $user->roles[0], array( 'administrator') ) ) {

        return admin_url();

    } elseif ( in_array( $user->roles[0], array( 'subscriber') ) ) {

        return $request;
    } 

    return $redirect_to;
}

Update 1:

Code (above) has been modified above and tested - it works.

Update 2: (by Tanya)

Chatted with @userabuser and came up with this answer that works.

// Redirect to previous page 
remove_all_filters('login_redirect', 99);
add_filter('bbp_user_login_redirect_to', 'function_name');

Important note:

It turns out that Tanya was using the bbPress forum plugin, therefore despite the original snippet which uses,

add_filter('login_redirect', 'function_name'); //the standard WordPress API filter

which is correct and does work under "normal" WordPress conditions, was, in this case being overwritten in priority by the bbPress filter,

add_filter('bbp_user_login_redirect_to', 'function_name'); //the bbPress API filter

Now since the documentation on the bbPress.org website which details these actions and hooks is poorly organized and somewhat incomplete (far as I can tell), I suggest that (anyone) seeking further information about what hooks where and what filters what, should take a look at this resource here which provides a detailed list of hooks and filters.

Notes: (miscellaneous)

Change 'subscriber' to match the role you are using for that type of user. It can also accept multiple roles since its in the form of an array.

in_array( $user->roles[0], array( 'subscriber', 'role2, role3', 'etc')
share|improve this answer
    
@Tanya Edited code to include the optional parameters. $user may not be accessible. Try this update and let me know. –  userabuser Oct 4 '12 at 9:10
    
@Tanya Hi Tanya, thanks for the update. Meanwhile, can you please delete your last comments (click the little x upon hover) so we can clean up the comment thread. I'll also be removing my irrelevant comments. –  userabuser Oct 12 '12 at 6:40
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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