admin管理员组

文章数量:1279174

I am currently using the wp-members wordpress plugin. I just learned that they have no functionality to redirect the user to the last page viewed after login or registration .. which has a solution thank you

I am currently using the wp-members wordpress plugin. I just learned that they have no functionality to redirect the user to the last page viewed after login or registration .. which has a solution thank you

Share Improve this question asked Oct 2, 2021 at 15:16 flexi2202flexi2202 175 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

Give this a try...

function save_url_before_login() {
  if(!is_user_logged_in()) {
    $_SESSION['referer_url'] = get_the_permalink();
}
add_action('wp', 'save_url_before_login');

and:

function redirect_after_login() {
  $redirect_url = home_url('/');
  if (isset($_SESSION['referer_url'])) {
    $redirect_url = $_SESSION['referer_url'];
    unset($_SESSION['referer_url']);
  }
  return $redirect_url;
}
add_filter('login_redirect', 'redirect_after_login');

I use this code on functions.php

// redirects user after login and logout
if (! function_exists('ctm_login')) {
    add_action('wp_login', 'ctm_login');
    function ctm_login(){wp_redirect($_POST['link']);exit;}
}
if (! function_exists('ctm_logout')) {
    add_action('wp_logout', 'ctm_logout');
    function ctm_logout(){wp_redirect($_POST['link']);exit;}
}

however this works because I have a input hidden on the login form

<input type="hidden" name="link" value="<?php echo home_url(add_query_arg(array($_GET), $wp->request));?>">

本文标签: pluginsHow to redirect users to the last page viewed