Using built in WordPress redirect_to functionality
On a page you want to redirect back to after login
$redirect_to = home_url(add_query_arg([], $_SERVER['REQUEST_URI']));
wp_redirect(wp_login_url($redirect_to));
exit;
If you’re placing the login form of your home page (or some other page to the default)
Redirect wp-login.php
//******************************************************************
//********** REDIRECT WP LOGIN PAGE TO OUR SITE HOME PAGE **********
//******************************************************************
//Fires page : /wp-login.php
//This method keeps the wp-login functionality working, such as redirect_to, but causes the wp-login.php page to redirect to your chosen page with your login form
add_action('login_init', 'ValeBlockWpLoginPage');
function ValeBlockWpLoginPage()
{
$action = $_REQUEST['action'] ?? 'login';
$method = $_SERVER['REQUEST_METHOD'] ?? 'GET';
$allowed_actions =
[
'logout',
'lostpassword',
'rp',
'resetpass',
'register',
'postpass',
'confirmaction',
'reauth',
];
//Allow actual login POSTs
if ($method === 'POST' && $action === 'login')
return;
//Allow other core login-related actions
if (in_array($action, $allowed_actions, true))
return;
//Only block viewing the default login screen
if ($method === 'GET' && $action === 'login')
{
$redirect_to = '';
if (!empty($_GET['redirect_to']))
{
$redirect_to = wp_validate_redirect(
wp_unslash($_GET['redirect_to']),
''
);
}
$target = home_url('/'); //<<<<<<The page with your login form on it
if ($redirect_to !== '')
{
$target = add_query_arg(
[
'redirect_to' => rawurlencode($redirect_to)
], $target);
}
wp_safe_redirect($target);
exit;
}
}
On a page you want to redirect back to after login
$target = home_url(add_query_arg([], $_SERVER['REQUEST_URI']));
$login_url = add_query_arg(
[
'redirect_to' => rawurlencode($target)
], home_url('/')
);
wp_safe_redirect($login_url);
exit;
Catch for page with your login form
//********** TEMPLATE PAGE ABOUT TO LOAD **********
add_action("template_redirect", 'mysite_template_redirect');
function mysite_template_redirect()
{
if (
(is_user_logged_in()) &&
(parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH) == '/') && //<<<<Login page
(!(str_contains($_SERVER["REQUEST_URI"], 'elementor-preview'))) //Don't get in the way of Elementor page editor previewing
)
{
$redirect_to = home_url('/somepage'); //<<<Default page you want to redirect to after login
//Check for redirect to previous page after login
if (!empty($_GET['redirect_to']))
{
$redirect_to = wp_validate_redirect(
wp_unslash($_GET['redirect_to']),
home_url('/')
);
}
wp_redirect($redirect_to);
die();
}
}
USEFUL?
We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through resources like this. We hope you find it helpful.
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support here. If you need help with a problem please use one of the many online forums.
