1

I want users "subscribers" to be redirected to the page they last viewed. At the moment, users are redirected to their "myaccount" profile page (my-account.php). I am trying to figure out how to adjust what I have already, or where to insert some script from another post on here.

I am fairly sure there is no database work to do here, its a only a change to the script, right?

Can anyone please check this wp-login.php script, and let me know what to change? I also have 2 functions.php files within the public_html structure.

Thank you in advance...

$requested_redirect_to = isset( $_REQUEST['redirect_to'] ) ? $_REQUEST['redirect_to'] : '';
        /**
         * Filters the login redirect URL.
         *
         * @since 3.0.0
         *
         * @param string           $redirect_to           The redirect destination URL.
         * @param string           $requested_redirect_to The requested redirect destination URL passed as a parameter.
         * @param WP_User|WP_Error $user                  WP_User object if login was successful, WP_Error object otherwise.
         */
        $redirect_to = apply_filters( 'login_redirect', $redirect_to, $requested_redirect_to, $user );

        if ( ! is_wp_error( $user ) && ! $reauth ) {
            if ( $interim_login ) {
                $message       = '<p class="message">' . __( 'You have logged in successfully.' ) . '</p>';
                $interim_login = 'success';
                login_header( '', $message );

                ?>
                </div>
                <?php

                /** This action is documented in wp-login.php */
                do_action( 'login_footer' );

                if ( $customize_login ) {
                    ?>
                    <script type="text/javascript">setTimeout( function(){ new wp.customize.Messenger({ url: '<?php echo wp_customize_url(); ?>', channel: 'login' }).send('login') }, 1000 );</script>
                    <?php
                }

                ?>
                </body></html>
                <?php

                exit;
            }

1 Answer 1

0

First word of advise, do not modify Wordpress core files ! It is bad practice and all your changes will be deleted on the next Wordpress update. Only make changes to the wp-content folder.

I believe you could use javascript and cookies to save the the last page visited before your user logged in.

I have had a similar issue recently on a shop where I wanted the users to be able to go back to the latest product archive visited by clicking a back button even after navigating through single products and related products etc...

Below is the js I used. That would go to your log in page

// Get the previous page visited URL
var prevURL = document.referrer;

// Function to set the cookie
function setCookie(cname, cvalue, exdays) {
    var d = new Date();
    d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
    var expires = "expires="+d.toUTCString();
    document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}

// Now set the cookie with this URL
setCookie("url", prevURL, 1);


// Function to read cookies
function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

// Read the cookie and redirect to the URL on click
var url = readCookie('url');

jQuery('a#backToShop').on('click', function(e){
    e.preventDefault();
    window.open(url , "_self");
});

You might want to change that last jQuery "click" function for something appropriate to the behavior needed, probably an action in your functions.php of your child theme to redirect after wp_login

Hope this helps !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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