You can achieve this by modifying your custom_login_redirect function to include a conditional check based on a parameter you pass from your specific page. Here’s an example of how you can modify your function:
function custom_login_redirect($redirect_to, $request, $user) {
// Check if there is a user and if the user has roles
if (isset($user->roles) && is_array($user->roles)) {
// Check if the login request comes from your specific page
if (isset($_GET['from']) && $_GET['from'] == 'special-page') {
// Do not redirect
return $redirect_to;
} else {
// Redirect to '/main_report' or another specified page
return home_url('/main_report');
}
} else {
// Default redirect if there's no user
return $redirect_to;
}
}
add_filter('login_redirect', 'custom_login_redirect', 10, 3);
If modifying the login form is not possible, or if you prefer not to use GET parameters for tracking the origin of a login request, you can use PHP session variables as an alternative. This method involves setting a session variable when a user visits your specific page and then checking this variable during the login process.
function set_login_origin_session() {
if (is_page('your-specific-page-slug')) {
// Start the session if it hasn't already been started
if (!session_id()) {
session_start();
}
// Set a session variable to indicate the user came from the specific page
$_SESSION['login_origin'] = 'specific-page';
}
}
add_action('template_redirect', 'set_login_origin_session');
function custom_login_redirect($redirect_to, $request, $user) {
// Check if the session variable is set and equals 'specific-page'
if (isset($_SESSION['login_origin']) && $_SESSION['login_origin'] === 'specific-page') {
// Clear the session variable to prevent unintended redirects in future logins
unset($_SESSION['login_origin']);
// Do not redirect; return the original destination
return $redirect_to;
} else {
// Redirect to your default location
return home_url('/main_report');
}
}
add_filter('login_redirect', 'custom_login_redirect', 10, 3);