I am currently working on a redirect function.
At the moment users must be logged in to view any page of the site.
If a user goes to an URL directly, for example: www.mysite.com/?p=3467373
, it will redirect them to a custom login page which will be something like this: www.mysite.com/login
. Once they have logged in I would like the user to be redirected to the page they originally tried to access.
At the moment I have this:
if (!is_user_logged_in() && is_single()){
$_SESSION['HolUrl'] = $_GET["p"];
header('Location: http://www.mysite.com/login/?'.$_SESSION['HolUrl']);
}
This works fine and passes over the variable p
.
The part I am having problems with is the login redirect. I have this set up in my functions.php
but for some reason it doesn’t grab the variable p
:
function my_login_redirect( $redirect_to, $request, $user ){
global $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 home_url().'/wp-admin';
} else {
return home_url().'/?p='.$_GET['p'];
}
}
else {
return $redirect_to;
}
}
add_filter("login_redirect", "my_login_redirect", 10, 3);
Can anyone see any problems with the above?