5

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.

NB:

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

/* SET NEW USER COOKIE */
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);

and

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

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

2 Answers 2

2

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/" );
    endif;

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" );
    endif;

endif;

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.

1
  • 3
    Why don't you use the is_page() function from WordPress? It's way more elegant than $_SERVER requests.
    – Ciprian
    Commented Sep 6, 2012 at 22:49
2

It is a best pratice to put such functionality in your themes functions.php as an action f.e.

add_action('init', 'your_own_function', 1);

function your_own_function(){

  $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/" );
    endif;

  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" );
    endif;

  endif;

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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