Home > Back-end >  redirect to reffer page after login wordpress
redirect to reffer page after login wordpress

Time:07-27

i'm using this code to redirect not logged users from a specific page to login page

add_action( 'template_redirect', function() {

if ( is_user_logged_in() || ! is_page() ) return;

$restricted = array( 5049 ); // all your restricted pages

if ( in_array( get_queried_object_id(), $restricted ) ) {
  wp_redirect( site_url( '/user-account' ) ); 
  exit();
  
}});

after login users are redirect to their account i want to change this to redirect them to previous page that they visited

CodePudding user response:

$_SERVER['HTTP_REFERER'] should hold the last visited page, if any.

<?php
if ( is_user_logged_in() || ! is_page() ) return;

$restricted = array( 5049 ); // all your restricted pages

if ( in_array( get_queried_object_id(), $restricted ) ) {
  $previous_url = $_SERVER['HTTP_REFERER'] ? : site_url( '/user-account' );
  wp_redirect( $previous_url); 
  exit();
  
}});
  • Related