Site icon Hip-Hop Website Design and Development

Disable redirect to homepage after successful Login, Stay on Current Page

I have built a WordPress login form using Bootstrap Modal.

When logging in it redirects you to the homepage after a successful login. I want to DISABLE this redirect. I just want the page you are login in from to be the page you see after login.

I’ve tried multiple things and cannot get it to work. When logging in it redirects you to the homepage on a successful login attempt. I would like this to stay on the page the user logged in from. Here is the code I’m using-

                    <form action="/wp-login.php" method="post" name="loginform">
                        <div class="row center" style="padding-top:10px; padding-bottom:10px;">
                            <div class="form-field">
                                <label>Username:</label>
                                <input type="text" class="login-username" name="log" />
                            </div>
                        </div>
                        <div class="row center" style="padding-top:10px; padding-bottom:10px;">
                            <div class="form-field">
                                <label>Password:</label>
                                <input type="password" class="login-password" name="pwd" />
                            </div>
                        </div>
                        <div class="row center" style="padding-top:10px; padding-bottom:10px;">
                            <div class="form-field">
                                <label for="rememberme" class="mm-remember-me">
                                    <input name="rememberme" id="rememberme" type="checkbox" checked="checked" value="forever">
                                    Remember me
                                </label>
                            </div>
                        </div>
                        <div class="row center">
                            <button type="submit" name="wp-submit" class="btn btn-primary btn-block">Login</button>
                        </div>
                        <input type="hidden" name="redirect_to" value="/';echo $currentURL;echo'" />
                    </form>
                

I’ve tried a few different things to get this to work and nothing is working for me. How do I get the automatic redirect after login to stop? My desired result is to log in with the modal and stay on the current page you logged in from.

Other things ive tried that didnt work-

I added this to my functions.php and it didnt seem to do anything at all.

function custom_login_redirect( $redirect_to, $request, $user ) {
if ( isset( $user->roles ) && is_array( $user->roles ) ) {
if ( in_array( 'administrator', $user->roles ) || in_array( 'editor', $user->roles ) || in_array( 'author', $user->roles ) ) {
$redirect_to = admin_url();
} else if ( in_array( 'user', $user->roles ) || in_array( 'member', $user->roles ) ) {
$redirect_to = $_SERVER['REQUEST_URI'];
} else {
$redirect_to = $_SERVER['REQUEST_URI'];
}
}
return $redirect_to;
}
add_filter( 'login_redirect', 'custom_login_redirect', 10, 3 );