I want, as an exception, do a redirect if a user reaches a point where he needs to be logged in, to the login screen, and as soon as he logs in and certian conditions are fullfilled (a flag set as myappname_allow_redirect
) I want to redirect back to the last url the user was at
In some shortcode that requires the user to be logged in:
try{
$mus = new myappnameUserService();
}catch (myappnameNeedsLoginException $e){
global $wp;
//this is the easiest way supposedly to get the current URL see here https://wordpress.stackexchange.com/questions/274569/how-to-get-url-of-current-page-displayed
$thisUrl = home_url($wp->request);
$thisUrl = urlencode($thisUrl);
myappname_redirect_to_url('/wp-login.php?redirect_to=' . $thisUrl . '&myappname_allow_redirect=1');
}
functions.php
function myappname_login_redirect($redirect_to, $request, $user)
{
if ($user instanceof WP_User) {
if (isset($user->roles) && is_array($user->roles)) {
//check for admins
if (in_array('administrator', $user->roles)) {
// redirect them to the default place
return $redirect_to;
} else {
//if something is set her for exceptions, we allow regular redirect as well
if($_GET['myappname_allow_redirect']){
return $redirect_to;
}else{
//but normally we redirect to a standard url
return 'somethingelse';
}
}
} else {
return $redirect_to;
}
}
//is there a user to check?
}
add_filter('login_redirect', 'myappname_login_redirect', 10, 3);
first it seems to work, I get redirected to mydomain.com/wp-login.php?redirect_to=xxxx&myappname_allow_redirect=1
but
myappname_login_redirect($redirect_to, $request, $user)
$redirect_to
is empty, and so is $request
template_redirect
filter and usewp_redirect()
function directly.