0

We store user meta data on our website. I have a function hooked into the wp_login action to redirect the user to a form to update their meta if it's older than 1 year. If the user's information is up to date, I want to redirect to the page they were at when they first clicked "Log in". I haven't been able to figure this out though. For now, I just redirect to the home page (in the last "else" statement). Any ideas?

Thank you!

Edit: Essentially, I need some way to run a custom function that executes as the user is navigating to the login page through the link generated by the wp_loginout() function. This custom function should store the prior link, maybe via something like $redirect = $_SERVER["HTTP_REFERER"];

function check_user_info($username) { 
  $user = get_user_by('login', $username);
  // Get current user ID  
  $wp_user_id=$user->ID;  
  // Unix display of times
  $current_time_unix= time();
  //add_user_meta($wp_user_id,'test4','test5',TRUE);
  $last_update_unix=get_user_meta($wp_user_id, 'last_updated', TRUE);
  $next_update_unix=get_user_meta($wp_user_id, 'next_update_request', TRUE);  
  // Nice display of times 
  $current_time = date('F j, Y H:i:s',$current_time_unix);
  $last_update_time = date('F j, Y H:i:s',$last_update_unix);
  $next_update_time = date('F j, Y H:i:s',$next_update_unix);
  // Echo Last / Next update time
  // echo "current time: ".$current_time."<br>";
  // echo "last update: ".$last_update_time."<br>";
  // echo "next update: ".$next_update_time."<br>";

  // If meta doesn't exist, must be a new user through social login, redirect
  if (!$last_update_unix) {
    //echo 'redirect';
    wp_redirect("https://eyeguru.org/register/?type=new_user_from_social_login");
    exit;
  } 
  // if meta exists, and the user information is 1+ years old, redirect 
  if ($last_update_unix && $current_time_unix>$next_update_unix) {
    //echo 'redirect';
    wp_redirect("https://eyeguru.org/register/?type=update_expired_info");
    exit;
  }
  // if meta exists, and the user information is up to date, redirect to homepage
  else {
    wp_redirect("https://eyeguru.org/");
    exit;
  }

}
add_action('wp_login', 'check_user_info');
2
  • Let me know what exactly problem you have been facing with above code??? Commented Apr 25, 2020 at 3:38
  • Hi Tanmay. The above code works, but I need some way to have the last else statement redirect to the page immediately before the login screen, rather than the home page. I think I need to write a function to store the prior address as the user is navigating to the login screen. But can't find a hook to use. The problem with this code is that the wp_login hook runs after the user logs in.
    – Ben
    Commented Apr 25, 2020 at 19:25

1 Answer 1

0

You can simply get the page where the user is coming from using $_SERVER["HTTP_REFERER"] and replace your redirect to the homepage like below:

// if meta exists, and the user information is up to date, redirect to homepage
  else {
    $redirect = $_SERVER["HTTP_REFERER"];
    wp_redirect( $redirect );
    exit;
  }
1
  • Hi Jekayode, thanks for the suggestion. Unfortunately, this doesn't work. I think it's because the "check_user_info" function is hooked into "wp_login", which runs after the user actually logs in. Therefore, address used for referer is stored as the login page itself, rather than the page before the login page.
    – Ben
    Commented Apr 25, 2020 at 19:24

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.