// Shortcode for retrieving Session params
if ( ! shortcode_exists('get_cookie') ) {
 function readycat_get_cookie( $atts ) {
 // check for shortcode param
 if ( empty( $atts['key'] ) ) return 'Please specify a key=whatever_value in the shortcode.';
 $default = !empty( $atts['default'] ) ? $atts['default'] : '';

 if ( ! empty( $_GET[ $atts['key'] ] ) ) {

 $value = $_GET[ $atts['key'] ];

 } elseif ( ! empty( $_COOKIE[ $atts['key'] ] ) ) {

 $value = $_COOKIE[ $atts['key'] ];

 } else {
 $value = $default;
 }
 return esc_html( $value );
 }
 add_shortcode('get_cookie', 'readycat_get_cookie');
}

// add javascript to footer to parse utm url params into cookies. Using JS (not PHP) so landing pages can be cached.
function set_url_param_cookies() {
 print "<script>if(~location.search.indexOf('utm')){location.search.substr(1).split('&').forEach(function(p){document.cookie=p});}</script>";
}
add_action( 'wp_footer', 'set_url_param_cookies');

Leave a Comment