Setting a cookie might be the best option. In case the visitor had js disabled, I figured it best to set the cookie in the http header using php. However, it seems that init
is the last possible action WordPress will allow a cookie-setting function to hook into, but it fires too early for conditional tags to work so I resorted to js.
I used conditional tags in functions.php
so that only if a specific landing page template (i.e. page-5.php
) is called will the script load and execute. As opposed to using raw JavaScript I utilized the js library cookie.js.
add_action('wp_enqueue_scripts', 'this_enqueue_scripts');
function this_enqueue_scripts() {
if ( is_page(5) || is_page(7) ) :
wp_enqueue_script('cookie-script', plugins_url( 'js/cookie.min.js', __FILE__ ), null, true);
endif;
}
add_action('wp_footer', 'set_cookie_func');
function set_cookie_func() {
$post = get_post($post_id);
if ( is_page(5) || is_page(7) ) : ?>
<script type="text/javascript">
var referral = <?php echo '"' . $post->post_name . '"'; ?>;
cookie.set( 'landing_page', referral, {expires: 7} );
</script>
<?php endif;
}
On my site the referral urls provided are application form urls – whether the user immediately completes the form or clicks around a bit, the cookie has been set and can be provided to the administrator via a hidden field. *Note that the final part of this solution is specific to the Gravity Forms plugin. It’s very simple in GF to add a hidden input field and pass a cookie through it. In the form editor, check the box ‘Allow field to be populated dynamically’ and add the following snippet to your functions.php
. (Replace ‘landing_page’ in the filter name to match your field parameter and cookie name.)
add_filter( 'gform_field_value_landing_page', 'populate_landing_page_field' );
function populate_landing_page_field( $value ) {
return $_COOKIE['landing_page'];
}