2 comments

  1. Don’t bother with the simpleSessionGet, instead do what WordPress itself does and use a get parameter

    e.g.

    $current_url = $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
    wp_redirect( get_permalink(37).'?redirect_to='.$current_url);
    

    Then in your redirect, do something similar to:

    wp_redirect( $_GET['redirect_to'] );
    
  2. Thanks to @Tom J Nowell I came up with this, which keeps the essence of the code and leaves the URL much cleaner, beacuse in the majority of cases $_GET is not needed

    page-profile.php

    if( !is_user_logged_in() ) {    
        $redirect = url_add_parameter( get_permalink(37), 'redirect_to='.get_permalink() ); // Adds a parameter to the URL checks if '&' or '?' are being used already
        wp_redirect( $redirect );
        exit;
    }
    

    page-login.php

    // Set referer
    if( $_GET['redirect_to'] ) 
        $referer = $_GET['redirect_to']; //When the referer page was a redirection
    else
        $referer = wp_get_referer();
    
    if( !$referer || referer_is_login_page($referer) )
        $referer = get_bloginfo('url');
    
    simpleSessionSet('login_referer', $referer);
    
    ... 
    //Log the user in 
    ...
    
    if ( is_wp_error($user_id) )            
        $error = TRUE;
    else 
        wp_redirect( simpleSessionGet('login_referer', '') );
    

Comments are closed.