Advanced Cforms Techniques: Dynamically prepopulating a form field via URL

We use Oliver Seidel’s CForms plugin quite a bit around here, due to its robust functionality, and support for aggressive customization, such as posting to MethodCRM, and posting to a spreadsheet in Google Docs. Someone recently posted a question on their forums about how to prepopulate a form field using data passed in the URL. Since we’ve done that before, I figured I’d outline the steps below:

In this scenario, I wanted to pull the company name selected from an index as a value from the URL to prepopulate a contact form. I added this to the functions.php of my theme:

function get_company_name() {
$companyName = explode('=',$_SERVER['QUERY_STRING']) ;
$companyName = urldecode($companyName[2]);
return $companyName;

add_shortcode ('referrer', 'get_company_name');

This registers the shortcode [referrer], which when called, will check the current URL for a value in the second attribute field.

Next, I needed to have the contact button on the actual company profile setup to send that value in the URL. Following the previous code in functions.php, I added:

function connect_to_company() { 
	$CurrLang = $_GET["lang"];
	$query = '' . get_the_title();
	$connectString = add_query_arg(array('page_id' => '161' , 'company' => get_the_title(), 'lang' => $CurrLang), $query);
	if ($CurrLang == "fr") {
	$connectButton = 'Cliquez ici pour contacter cette entreprise';
} else {
	$connectButton = 'Click to contact this company';
	return $connectButton;

add_shortcode ('connect_button', 'connect_to_company');

I also check for language settings in this piece, because my use case is on a bilingual site. You can use this method to add as many values to the URL as needed, up to and including completely prepopulating your target form. The important parts are that I’m sending the page ID where the target form is located, and the payload data.

In CForms 14.1 and below, you can drop the referrer shortcode into an input field at this point, and it will render correctly. In 14.6, the filtering order changed, which prevents shortcodes from being fired off in the form load. The solution is to make the following change in cforms.php:

On or around line 1792, change:

add_filter('the_content', 'cforms_insert',101)


add_filter('the_content', 'cforms_insert',10);

And that’s it! Shortcodes can be used to pull any number of values and prepopulate them, including setting up dynamic greetings and fieldset names.

Questions? Feel free to leave a comment.