Wordpress - Redirect user to original url after login?

You can do that easily. You just need to specify a redirection parameter. If you are using a login link on the homepage to go to the login page, then @sisir's solution is correct.

<?php echo wp_login_url( $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"] ); ?>

If you are using a custom form on the frontpage, then inside the <form>, make sure you fill in a hidden field with the url to redirect

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

And if you are using wp_login_form() to generate the form, then fill in a parameter - http://codex.wordpress.org/Function_Reference/wp_login_form

<?php
$args = array(
        'echo' => true,
        'redirect' => site_url( $_SERVER['REQUEST_URI'] ), 
        'form_id' => 'loginform',
        'label_username' => __( 'Username' ),
        'label_password' => __( 'Password' ),
        'label_remember' => __( 'Remember Me' ),
        'label_log_in' => __( 'Log In' ),
        'id_username' => 'user_login',
        'id_password' => 'user_pass',
        'id_remember' => 'rememberme',
        'id_submit' => 'wp-submit',
        'remember' => true,
        'value_username' => NULL,
        'value_remember' => false );

wp_login_form( $args );
?>

Change other parameters as per what you have or need.


Try passing the_permalink() as the $redirect argument:

function restrict_access_if_logged_out(){
    if (!is_user_logged_in() && !is_home()){
        wp_redirect( the_permalink() );
    }
}
add_action( 'wp', 'restrict_access_if_logged_out', 3 );

EDIT:

Sorry, misunderstood your question originally. Try this:

function restrict_access_if_logged_out(){
    if (!is_user_logged_in() && !is_home()){
        $redirect = home_url() . '/wp-login.php?redirect_to=' . urlencode( $_SERVER['REQUEST_URI'] );
        wp_redirect( $redirect );
        exit;
    }
}
add_action( 'wp', 'restrict_access_if_logged_out', 3 );

Note also: proper usage of wp_redirect() generally requires adding exit;, which I have added to my second example.


Thanks all, I kind of used a bit of what everyone recommended so in the end my code looks like this:

function restrict_access_if_logged_out(){
    if (!is_user_logged_in() && !is_home()){
        wp_redirect( get_option('home') . '?redirect_to=' . esc_url($_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]) );
    }
}
add_action( 'wp', 'restrict_access_if_logged_out', 3 );

And on my login form (I'm hardcoding my login form in my aplication thanks @Ashfame for letting me know about wp_login_form I had no idea it existed) I added this when user credentials are fine and they're ready to login:

if (isset($_REQUEST['redirect_to'])){
    wp_redirect($_REQUEST['redirect_to']);
    // wp_redirect() does not exit automatically, and should almost always be followed by a call to exit;
    exit;

} else {
    wp_redirect(get_bloginfo('url') . '/groups/');
    exit;
}

Thanks a lot for your help, I voted up everyone!