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, "https://wordpress.stackexchange.com/", '.mydomain.com', false);
and
setcookie('newtcpa_visitor', 1, time()+3600*24*100, "https://wordpress.stackexchange.com/", '.mydomain.com/thank-you/', false);
Apologies for the multiple edits… first post on WordPress s
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 Best Answerecond, the thank you page refreshes, redirecting to the home page. The visitor can now view the site as normal.