Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to redirect on previous to previous page in wordpress

Tags:

php

wordpress

I am a newbie to wordpress. I am currently working on registration and login. I have a page restrict url let http://xyz.com/abc. Login/registration is required for this url.

I need that when a user register or login then it should be redirected on this previous url.

In login I have added the following hook :

a

add_filter('login_redirect', 'redirect_previous_page', 10);

function redirect_previous_page(){

    global $user;
    $request = $_SERVER["HTTP_REFERER"];

    if ( in_array( $user->roles[0], array( 'administrator') ) ) {

        return admin_url();
        return $redirect_to;

    } elseif ( in_array( $user->roles[0], array( 'subscriber') ) ) {

        return $request;
        return $redirect_to;

    } 

}

It is working for me, but I am not getting how can I redirect on this url after successful registration. I have registration link on the login page

like image 845
Rohitashv Singhal Avatar asked Oct 15 '25 03:10

Rohitashv Singhal


1 Answers

As mentioned in the docs, you can use: <?php wp_get_referer() ?> to get the last URL visited by user, before logout or after login.

like image 75
shasi kanth Avatar answered Oct 16 '25 19:10

shasi kanth



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!