// Add to your theme's functions.php or a custom plugin
/**
* Capture the referring URL before user navigates to the login page.
*/
function wbcom_capture_referring_url() {
// Check if the current page is the WordPress login page and the user is not logged in
if ( strpos($_SERVER['REQUEST_URI'], 'wp-login.php') !== false && !is_user_logged_in() ) {
// Get the referrer URL
$referrer = wp_get_referer();
// If there's a referrer, store it in a session variable
if ( !empty($referrer) ) {
$_SESSION['wbcom_login_referrer'] = $referrer;
}
}
}
add_action('wp', 'wbcom_capture_referring_url');
/**
* Redirect user to the previously captured URL after successful login.
*/
function wbcom_redirect_after_login( $redirect_to, $requested_redirect_to, $user ) {
// Check if our session variable with the referrer URL is set
if ( isset($_SESSION['wbcom_login_referrer']) ) {
// Store the URL
$redirect_url = $_SESSION['wbcom_login_referrer'];
// Unset the session variable for future logins
unset($_SESSION['wbcom_login_referrer']);
// Redirect to the stored URL
return $redirect_url;
}
// If no referrer URL was captured, proceed with the default redirect
return $redirect_to;
}
add_filter('login_redirect', 'wbcom_redirect_after_login', 10, 3);
Comments
0 B
|👍
/👎
0 B
|👍
/👎
0 B
|👍
/👎
0 B
|👍
/👎