WordPress hacks: passing custom session variables in cforms

Many a times people configure and run their entire website using WordPress.

cforms is an awesome feature rich plugin for WordPress, offering convenient deployment of multiple Ajax driven contact forms throughout the website.
Many wordpress based websites use cforms to capture and store form submissions on their site.

Recently came across a requirement where client is running his website on wordpress and also using online marketing campaigns (google, adnetworks)  to drive traffic to his website.

Requirement was to track those custom variables (source, creative name) passed to his website.

Many analytics tools should be able to do this, however he wanted to store the data in surfer cforms submissions in database as well as take some action based on his submission.

This can be achieved using following Steps:

Step 1)   Edit header.php of your wordpress theme and add following lines at the start of the file

if (isset($am_usrc)) {
$_SESSION["amSrc"] = $am_usrc;
$_SESSION["amUnit"] = $am_adunit;

Note the variable names and use them as per your variable names. Also you can store them in sessions/cookies/database as per your requirement.

Step 2) Now define the variable names in `lib_aux.php` file from plugins/cforms directory in following manner

### get variables from session and assign to your temp variables
$sessCampaignSRC = $_SESSION["amSrc"];
$sessCampaignAdUnit = $_SESSION["amUnit"];

$m  = str_replace( ‘{Referer}’,        $_SERVER['HTTP_REFERER'], $m );
$m  = str_replace( ‘{PostID}’,        $pid, $m );
$m     = str_replace( ‘{Form Name}’,    $cformsSettings['form'.$no]['cforms'.$no.'_fname'], $m );
$m     = str_replace( ‘{Page}’,        $page, $m );
$m     = str_replace( ‘{Date}’,        $date, $m );
$m     = str_replace( ‘{Author}’,        $find->display_name, $m );
$m     = str_replace( ‘{Time}’,        $time, $m );
$m     = str_replace( ‘{IP}’,            cf_getip(), $m );
$m     = str_replace( ‘{BLOGNAME}’,    get_option(‘blogname’), $m );

$m     = str_replace( ‘{CurUserID}’,    $CurrUser->ID, $m );
$m     = str_replace( ‘{CurUserName}’,    $CurrUser->display_name, $m );
$m     = str_replace( ‘{CurUserEmail}’,$CurrUser->user_email, $m );

$m     = str_replace( ‘{Permalink}’,    $permalink, $m );
$m     = str_replace( ‘{Title}’,        $find->post_title, $m );
$m     = str_replace( ‘{Excerpt}’,        $find->post_excerpt, $m );

### define your custome variables campaignSRC and campaignAdUnit to store the values from temporary variables
$m = str_replace( ‘{campaignSRC}’, $sessCampaignSRC, $m );
$m = str_replace( ‘{campaignAdUnit}’, $sessCampaignAdUnit, $m );

Note: the variable names and session retrieval … you can get them from cookies if you’ve stored them in cookie files
Also make sure variables used have unique names without conflicting with cforms variable names

Step 3) Use the above mentioned custom variable names in cforms while configuring hidden variables and their values.


Configure your form accordingly and insert into your normal post and you are done :-)

Working example of the same can be found here on following two links

Sample Click through url : http://mayavi.info/?source=google&adunit=mayavi_ads

Form url :  http://mayavi.info/index.php/lamp/cforms-test.html

Check the view source of form page and you’ll be able to see the above two tracking parameters in your hidden variables.

6 comments to WordPress hacks: passing custom session variables in cforms

Leave a Reply




You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>