skip to Main Content

Can anybody help with, what should be, a simple redirect issue in WordPress.

I have a couple of pages that are part of a sign-up process that I want to ‘hide’ unless accessed as part of that process.

As an example, the page ‘/join/payment-setup/’ needs to be inaccessible unless a visitor has come from the ‘/join/’ page – just a simple redirect to the ‘/join/’ page would suffice.

2

Answers


  1. I’m not a WordPress expert, but with a quick run through the documentation, I would probably do it with a mix of using sessions, wp_get_referer(), and wp_redirect().

    1. Start the session at the top of the page
    session_start()
    
    1. Check the wp_get_referer if it matches the /join/ page. If it does, save some session status for later use. This should be checked on every page to ensure it also gets reset. Notice that with this setup, if the user refreshes the page in the payment-setup page, they will lose access. So you would have to add some logic to deal with that.
    if (wp_get_referer() === home_url('/join/')) {
        $_SESSION['allowed'] = true;
    } 
    else {
        $_SESSION['allowed'] = false;
    }
    
    1. In the page you want to control the access, check if a user is allowed to go there, similarly to preventing access before the user signs in. If the user has not come from the right page, re-direct them to the correct page using wp-redirect and exit the code.
    if (!isset($_SESSION['allowed']) || !$_SESSION['allowed']) {
        wp_redirect(home_url('/join/'));
        exit;
    }
    
    Login or Signup to reply.
  2. Instead of checking the unreliable (and fakeable) referer information, you could do this:

    1. When the user visits /join/, store $_SESSION['allow_join'] = true;
    2. When the user visits /join/payment-setup/, check if ($_SESSION['allow_join'] == true), otherwise redirect user to the /join/ page.
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search