Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Gravity Forms Cookies // Saves the query string parameters from a users visit to a cookie and allows these values to be populated via Gravity Forms' Dynamic
<?php
/**
Plugin Name: Gravity Forms Cookies
Plugin URI: http://ounceoftalent.com/
Description: Saves the query string parameters from a users visit to a cookie and allows these values to be populated via Gravity Forms' Dynamic Population feature.
Version: 1.5
Author: David Smith
Author URI: http://ounceoftalent.com
License: GPL2
*/
class GFCookie {
public static $cookie_prefix = 'gfc_';
public static $session = false;
public static $fresh_cookies = array();
public static function init() {
//GFCookie::delete_cookies(); exit;
if(!is_admin()) {
add_action('parse_request', array('GFCookie', 'save_cookie'), 9);
add_action('gform_pre_render', array('GFCookie', 'load_cookie'));
}
add_shortcode('gf_cookied_link', array('GFCookie', 'cookied_link_shortcode'));
}
public static function save_cookie() {
// if cookies are not enabled, default to session use
// start the session regardless of $_GET being empty as session may have been previously populated
if(!self::cookies_enabled()) {
session_start();
}
// don't do anything with cookie if there is no data
if(!empty($_GET))
self::save_get_data();
self::save_server_data();
}
public static function load_cookie($form = null) {
$cookie = self::get_cookie_data(false);
foreach($cookie as $key => $value) {
// skip cookies that were not added by us
if(strpos($key, self::$cookie_prefix) === false)
continue;
// load cookie value into $_GET for population into Gravity Forms
$key = str_replace(self::$cookie_prefix, '', $key);
$_GET[$key] = $value;
}
// if gfc referrer is set, find {referer} merge tag and replace
if(self::get_cookie_data('HTTP_REFERER')) {
foreach($form['fields'] as &$field) {
if(strpos(rgar($field, 'defaultValue'), '{referer}') !== false)
$field['defaultValue'] = str_replace('{referer}', self::get_cookie_data('HTTP_REFERER'), $field['defaultValue']);
}
}
return $form;
}
public static function get_cookie_data($key = false) {
$session = isset($_SESSION) && is_array($_SESSION) ? $_SESSION : array();
$cookie = self::cookies_enabled() ? $_COOKIE : $session;
// cookies that have not yet been sent through the header are merged with existing cookies
$cookie = array_merge($cookie, self::$fresh_cookies);
if($key === false)
return $cookie;
$gfc_key = self::$cookie_prefix . $key;
return rgar($cookie, $gfc_key);
}
public static function save_get_data() {
$reserved_words = array('attachment_id', 'author', 'author_name', 'calendar', 'cat', 'category', 'category__and', 'category__in', 'category__not_in', 'category_name', 'comments_per_page', 'comments_popup', 'cpage', 'day', 'debug', 'error', 'exact', 'feed', 'hour', 'link_category', 'm', 'minute', 'monthnum', 'more', 'name', 'nav_menu', 'nopaging', 'offset', 'order', 'orderby', 'p', 'page', 'page_id', 'paged', 'pagename', 'pb', 'perm', 'post', 'post__in', 'post__not_in', 'post_format', 'post_mime_type', 'post_status', 'post_tag', 'post_type', 'posts', 'posts_per_archive_page', 'posts_per_page', 'preview', 'robots', 's', 'search', 'second', 'sentence', 'showposts', 'static', 'subpost', 'subpost_id', 'tag', 'tag__and', 'tag__in', 'tag__not_in', 'tag_id', 'tag_slug__and', 'tag_slug__in', 'taxonomy', 'tb', 'term', 'type', 'w', 'withcomments', 'withoutcomments', 'year', 'DBGSESSID');
foreach($_GET as $key => $value) {
if(!in_array($key, $reserved_words)) {
self::save_data($key, $value);
}
}
}
public static function save_server_data() {
$server_keys = array('HTTP_REFERER');
foreach($_SERVER as $key => $value) {
if(in_array($key, $server_keys))
self::save_data($key, $value);
}
}
public static function save_data($key, $value) {
$gfc_key = self::$cookie_prefix . $key;
if(self::cookies_enabled()) {
if(!rgar($_COOKIE, $gfc_key)) {
setcookie($gfc_key, $value, time() + 60*60*24*60, '/');
self::$fresh_cookies[$gfc_key] = $value;
}
} else {
if(!rgar($_SESSION, $gfc_key)) {
$_SESSION[$gfc_key] = $value;
self::$fresh_cookies[$gfc_key] = $value;
}
}
}
/**
* WP uses cookies by default so if $_COOKIES global is empty, cookies are likely not enabled
*
*/
public static function cookies_enabled() {
return !empty($_COOKIE);
}
public static function delete_cookies() {
foreach($_COOKIE as $key => $value) {
if(strpos($key, self::$cookie_prefix) !== false) {
self::delete_cookie($key);
}
}
}
public static function delete_cookie($key) {
setcookie($key, null, time() - 3600, '/');
}
public static function cookied_link_shortcode($atts) {
extract( shortcode_atts( array(
'url' => false
), $atts ) );
return !$url ? '' : gf_get_cookied_link($url);
}
}
GFCookie::init();
if(!function_exists('gf_get_cookied_link')) {
function gf_get_cookied_link($url) {
$cookies = GFCookie::get_cookie_data();
$gfc_cookies = array();
foreach($cookies as $key => $value) {
if(strpos($key, GFCookie::$cookie_prefix) !== false) {
$key = str_replace('gfc_', '', $key);
$gfc_cookies[$key] = str_replace(array('http://', 'https://'), '', $value);
}
}
return add_query_arg($gfc_cookies, $url);
}
}
@alexphelps

This comment has been minimized.

Copy link

alexphelps commented Apr 28, 2014

@spivurno - this looks pretty great. How do you use a custom query parameter like ?affiliate_id=12356 to save to populate a hidden field?

@lucanos

This comment has been minimized.

Copy link

lucanos commented May 27, 2016

@Pimento

This comment has been minimized.

Copy link

Pimento commented Aug 30, 2017

Would love some basic direction on how to implement this.

@maan1234

This comment has been minimized.

Copy link

maan1234 commented Apr 10, 2020

Thank, works almost great out of the box! Saves me a lot of coding.

Found one issue: the $_COOKIE can be empty, not because cookies are disabled, but because the user is new and has received no cookies. The snippet starts a session and then fails to load from the session, because $_COOKIE is populated in the second page request. Then cookies are used instead of the session. The saved values in the session are thus not read...
Quick fix, just have cookies_enabled() return true. (if you know cookies are enabled on the server, which usually is)

Would love some basic direction on how to implement this.

Had some difficulty finding the easy way myself. Tip: caching can skip the execution of the snippet. So make sure there's no caching when URLs have GET params.

With FTP, create a new folder in the plugins folder (wp-includes/plugins/) and upload the snippet. The snippet appears in the plugin list in de WP admin. Activate it. When you create a folder, check the dynamic population and enter the query param that you need.

@spivurno

This comment has been minimized.

Copy link
Owner Author

spivurno commented Apr 11, 2020

@mann1234 Glad you found this useful!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.