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?

  • You are actually just passing the value of the variable. Your »single-to-login-redirect«-URL should look like this http://www.mysite.com/login/?3467373. So the problem would be that the $_GET['p'] isn't available. Besides, you don't need to pass it, because you have setup up the the session variable - $_SESSION['HolUrl'] - for this, so just use it for your login redirect. – Nicolai Apr 2 '14 at 14:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.