WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am looking for a way to solve the following problem:

User should only be able to visit mywebsite.com/page2/ after they visited mywebsite.com/page1/

If a user was not on page1, s/he should not be able to visit page2.

Is this possible?

Further specification: On page1 should be a survey (plugin: "Contact Form 7") and people are redirected to page2 after submitting their answers.

share|improve this question
You could also use codex.wordpress.org/WordPress_Nonces in the url. – jgraup Jan 6 at 17:15

You could set a cookie upon contact form submission, then check for the cookie on /page2. If it is not found, wp_safe_redirect() back to /page1.

Setting a cookie - You will need to hook into Contact Form 7's submission - look into their documentation:

setcookie( 'my_cookies_name', true, 0 ); // This cookie will expire after the session

Retreiving the cookie (on /page2's php template:

if( is_page('page2') ) {
    // Because the value is set to true, you can just check for the value in if()
    if( ! $_COOKIE['my_cookies_name'] ) {
        // cookie isn't found, redirect back
        wp_safe_redirect( site_url('/page1') );
share|improve this answer
Sounds good! :) – kbrunner Oct 31 '12 at 17:13
How can I set the cookie? – kbrunner Oct 31 '12 at 17:13
Edited my answer. – Eric Holmes Oct 31 '12 at 17:20
As far as I see, I have to set the cookie in the functions-template of the contact form 7 plugin. But where I don't get, where and how I can retrieve the cookie on the second page? To make it clearer: Where do I have to insert the second code snippet? – kbrunner Nov 7 '12 at 17:51
You can enter it into your functions.php file. with a if( is_page( 'page2' ) if statement wrapped around it. Or you can create a template for page2. Duplicate the page.php file within your Theme, and call it page-page2_slug.php where page2_slug is the page's actual slug. – Eric Holmes Nov 8 '12 at 18:07

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.