How to redirect WordPress user to previous page after login

Solution:

If you are using a login link on the homepage to go to the login page, then use the following code,

echo wp_login_url( $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"] );

If you are using a custom form on the frontpage, then inside the , 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 follow the below code, for more information checkout this page, wp_login_form

$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 );