Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm trying to direct new site visitors to a custom registration page for the their first visit only. The registration collects detailed information relating to an upcoming event, it is not designed to register the visitor as a user of the WordPress installation.

The registration page contains a Gravity form which when submitted redirects to a thank you page.

I would like to know the correct way to place a cookie in WordPress only when the visitor lands on the thank you page.

Thanks in advance.


I should also say that I've added the following to the functions.php file:

function set_newuser_cookie() {
    if (!isset($_COOKIE['newtcpa_visitor'])) {
        setcookie('newtcpa_visitor', 1, time()+3600*24*100, COOKIEPATH, COOKIE_DOMAIN, false);
add_action( 'init', 'set_newuser_cookie');

I'm unsure about COOKIEPATH & COOKIE_DOMAIN. I've tried the following settings, but I'm not having much luck at isolating the WP template/page:

setcookie('newtcpa_visitor', 1, time()+3600*24*100, '/', '.mydomain.com', false);


setcookie('newtcpa_visitor', 1, time()+3600*24*100, '/', '.mydomain.com/thank-you/', false);

Apologies for the multiple edits... first post on WordPress Answers

share|improve this question

1 Answer 1

I've worked out a solution. There may be a more elegant way to achieve this, but I had trouble getting anything else to work.

I've placed the following code at the beginning of the header.php file for all pages. The objective is to funnel site visitors through an event registration page, prior to accessing the site's content:

$currentURL = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];

if ( $currentURL == 'http://mydomain.com/website-registration/' ) :

elseif ( $currentURL != 'http://mydomain.com/thank-you/' ) :

    if ( ! isset( $_COOKIE["tcpa_newvisitor"] ) ) :
        header( "Location: http://mydomain.com/website-registration/" );

else :

    if ( ! isset( $_COOKIE['tcpa_newvisitor'] ) ) :
        setcookie( 'tcpa_newvisitor', 1, time() + 3600 * 24 * 100, COOKIEPATH, COOKIE_DOMAIN, false);
        header( "refresh:1;url=http://mydomain.com" );


The code collects the current page in $currentURL. If the current page is the target website registration page, then nothing happens. If the URI is anything other than the thank you page, and the cookie has not yet been set, the browser redirects to the registration page. When the form on the registration page has been submitted, the user is directed to the thank you page where the cookie is set. Finally, after 1 second, the thank you page refreshes, redirecting to the home page. The visitor can now view the site as normal.

share|improve this answer
Why don't you use the is_page() function from WordPress? It's way more elegant than $_SERVER requests. –  Ciprian Sep 6 '12 at 22:49

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.