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 am using a custom wordpress login form using the following code. I have this form in the sidebar of my site:

<?php
if ( ! is_user_logged_in() ) { // Display WordPress login form:
    $args = array(
        'redirect' => admin_url(), 
        'form_id' => 'loginform-custom',
        'label_username' => __( 'Username' ),
        'label_password' => __( 'Password ),
        'label_remember' => __( 'Remember Me' ),
        'label_log_in' => __( 'Log In' ),
        'remember' => true
    );
    wp_login_form( $args );
} else { // If logged in:
    echo 'you are already logged in';
    wp_loginout( home_url() ); // Display "Log Out" link.
    echo " | ";
    wp_register('', ''); // Display "Site Admin" link.
}
?>

And in the functions.php page, I am using the following code to redirect users to the homepage after they log in:

function my_login_redirect( $redirect_to, $request, $user ){
    //is there a user to check?
    if( isset( $user->roles ) && is_array( $user->roles ) ) {
        //check for admins
        if( in_array( "administrator", $user->roles ) ) {
            // redirect them to the default place
            return $redirect_to;
        } else {
            return home_url();
        }
    }
    else {
        return $redirect_to;
    }
}
add_filter("login_redirect", "my_login_redirect", 10, 3);

My question is:, Is there a way to redirect users to the 'referrer page (page from where they logged in)' after they login by making changes to the above code or perhaps using a better code?

Thanks in advance.

share|improve this question
    
Who do you want to redirect? Administrators? Everybody else? Both? –  s_ha_dum Apr 3 '13 at 20:09
    
@s_ha_dum both administrators and users –  theshorttree Apr 3 '13 at 20:48

1 Answer 1

The key part is right here-- 'redirect' => admin_url(), in your first block of code. Whatever you set that to is where you will redirect. I don't see a reason for the second block if you are redirecting everyone.

There is no function that I can find for "this page" so you probably want $_SERVER['REQUEST_URI']

$args = array(
    'redirect' => esc_url($_SERVER['REQUEST_URI']), 
    'form_id' => 'loginform-custom',
    'label_username' => __( 'Username' ),
    'label_password' => __( 'Password' ),
    'label_remember' => __( 'Remember Me' ),
    'label_log_in' => __( 'Log In' ),
    'remember' => true
);
wp_login_form( $args );

Be careful with $_SERVER vars: http://markjaquith.wordpress.com/2009/09/21/php-server-vars-not-safe-in-forms-or-links/

share|improve this answer
    
thanks a lot, will check the code and post back here if it works for me. And will check out that link too. –  theshorttree Apr 4 '13 at 12:34

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.