How to redirect user to previous accessed page after successful login/register in word press

hi, i am using word press to make one site, so i want one functionality in which if user go to suppose blog page mysitename/blog/ he will be redirected to login page say mysitename/login/ and after successfull login/ register the person should be redirected to previous page that is mysitename/blog/ and this should be dynamic for every page i want.

Currenlty i have tried using 

if ( !is_user_logged_in() ) {

header("location:mysitename/login/");

}

in blog page which redirects user to login page

but i dont know how to redirect them to previous accessed page.

any help will be appreciated

Replies

  • Kaustubh Katdare
    Kaustubh Katdare

    #-Link-Snipped-# - If the login happens on some other page than the current page, you will have to store the HTTP REFERER in cache or database temporarily. WordPress has wp_get_referer() function which will give you the page that referred. 

    You could then simply make use of: wp_safe_redirect( wp_get_referer() );

    Hope this helps. 

You are reading an archived discussion.

Related Posts

I'm electical graduate and I have 2 yr work experience in switchyard maintenance as deputy site incharge, but now  I m finding new job like this , or which course...
I completed b.tech .but I have a backlogs in b.tech , I have sufficient knowledge on programming ,so i am interested to get a job in MNC . Is there...
stucked while studying due to various doubts come to my mind at that time
Manufacturer uses 8 cylinder for 3200cc engine. why they are not using single big cylinder Having 3200 cc engine capacity?
I want to to develop mini website for college management system in html with php MySQL how can i proceed it . please help me mam.How can develop that college...