• Resolved slapshot36227

    (@slapshot36227)


    What I’d like to do is restrict a few of my pages so that they can only be accessed through clicking a link. For example, https://www.yourdomain.com/subscribe/subscribed-url should only be accessed when redirected to by clicking a link on my website. I don’t want people to be able to copy and paste that into their browser and access the page. Is this possible?

    I know this is possible for .php files, but I can’t figure out how to do it within WordPress pages. I read some stuff about editing the page template files (I’d create a new template for pages like this) but I couldn’t entirely get the code working properly, all I accomplished was restricting that actual template file from being accessed.

    Thanks,
    Zach

Viewing 6 replies - 1 through 6 (of 6 total)
  • Timothy Jacobs

    (@timothyblynjacobs)

    There is no reliable way to do this. What you want to do is check the $_SERVER global variable, like so:

    if ( ! isset( $_SERVER['HTTP_REFERER'] ) || empty( trim( $_SERVER['HTTP_REFERER'] ) )
            wp_redirect( home_url() );

    Then use a php in wp pages plugin and just pop that snippet in in any page you want to have that.

    Thread Starter slapshot36227

    (@slapshot36227)

    I have gotten the first half to work but the second part:

    || empty( trim( $_SERVER['HTTP_REFERER'] ) )

    gives me this error: “Can’t use function return value in write context in…”

    I changed the code to the following, based on what I found here: https://stackoverflow.com/questions/4192281/error-message-fatal-error-cant-use-function-return-value-in-write-context-i

    $test = trim($_SERVER['HTTP_REFERER']);
    if ( ! isset( $_SERVER['HTTP_REFERER'] ) || empty($test))
    	wp_redirect( home_url() );

    Everything seems fine but let me know if you see any problems.

    Thanks.

    Thread Starter slapshot36227

    (@slapshot36227)

    ^^Edited

    Timothy Jacobs

    (@timothyblynjacobs)

    Whoops, my bad. Change it one more time so you don’t get php warnings. You want to check if a value is set before you assign it to a variable

    if ( isset( $_SERVER['HTTP_REFERER'] ) ) {
            $refer = trim( $_SERVER['HTTP_REFERER'] );
             if ( empty( $refer ) {
                     wp_redirect( home_url() );
                     exit();
             }
    } else {
              wp_redirect( home_url() );
              exit();
    }
    Thread Starter slapshot36227

    (@slapshot36227)

    Thanks again ??

    I’m trying to accomplish this same thing. I have a couple questions (sorry if these are simple questions)…

    1. Do I need to replace HTTP_REFERER and home_url with my own urls? Are they cap sensitive?

    2. Do I only place that code into the plugin you mentioned, or does it also go somewhere else?

    Thank you!

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Restrict Direct Access to Certain Pages’ is closed to new replies.