通过wp_登录挂钩将登录后的用户重定向到上一页

时间:2020-04-25 作者:Ben

我们在网站上存储用户元数据。我有一个连接到wp\\u登录操作的函数,可以将用户重定向到一个表单,以便在其元数据超过1年时更新其元数据。如果用户的信息是最新的,我想重定向到他们第一次单击“登录”时所在的页面。但我还没能弄明白这一点。现在,我只重定向到主页(在最后的“else”语句中)。有什么想法吗?

非常感谢。

编辑:本质上,我需要某种方式来运行自定义函数,该函数在用户通过wp\\u loginout()函数生成的链接导航到登录页面时执行。这个自定义函数应该存储先前的链接,可能通过以下方式$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\');

1 个回复
SO网友:Jekayode

您只需使用$_SERVER["HTTP_REFERER"] 并替换重定向到主页,如下所示:

// if meta exists, and the user information is up to date, redirect to homepage
  else {
    $redirect = $_SERVER["HTTP_REFERER"];
    wp_redirect( $redirect );
    exit;
  }

相关推荐

301 Redirection After Comment

下面的代码将访问者重定向到您想要的任何页面。// Redirect to thank you post after comment add_action(\'comment_post_redirect\', \'redirect_to_thank_page\'); function redirect_to_thank_page() { return \'https://example.com/thank-you\'; } 但是,我使用WP Multisite