Say you want to allow users to contact you, and you want to provide some context for their inquiry. For example, you might want to know where they came from before they sent you their message. You can achieve this with a combination of Contact Form 7 and my plugin, Contact Form 7 Dynamic Text Extension.
This tutorial assumes you have already installed the Contact Form 7 and Dynamic Text Extension plugins.
The Contact Page
First thing you’ll need is a Contact Page. Create a new WordPress page and call it something exciting like “Contact”.
The Contact Form
Next, you’ll need to create a Contact Form. Navigate to Contact > Edit in your Control Panel. You’ll get a default form with your basic fields: Name, Email, Subject, Message.
To provide some context to the form, we’ll add a new field. We’ll call it “Context”, but you could call it something more specific (like “Product” or “URL” or whatever). To do this, we add a Dynamic Text Field that will pull the context from the GET variables in the URL (we’ll set those up in the next step). The GET variable will be called “context”.
You can use the WPCF7 Tag Generator to create the Dynamic Text field.
Name | contact-form-context |
id | contact-form-context |
Make this field uneditable | checked Keeps the user from changing this on you |
Dynamic Value | CF7_GET key=”context” This tells the form to grab the context GET variable from the URL. |
The tag ends up looking like this:
[dynamictext contact-form-context id:contact-form-context uneditable "CF7_GET key='context'"]
Take the shortcode and place it in the Form Text with a label like “Context”
<p>Context <br/> [dynamictext contact-form-context id:contact-form-context uneditable "CF7_GET key='context'" ] </p>
Save your form. Copy the shortcode it generates for you and place it in the text of the Contact page you created, for example (surround this with square brackets []):
contact-form 1 "Contact form 1"
Linking to the Contact Page
Now all that’s left is to pass the context
variable to the Contact Form. Say you have a bunch of posts (or pages) and when a user contacts you, you want to know what page they clicked “contact” on.
For each page you want this on, you just create a link to the Contact page with the proper variable set in the URL. Use the following format:
<?php echo '<a href="http://mysite.com/contact?context='. urlencode("My Page About Aardvarks").'" >Contact Us</a>'; ?>
Where contact is the slug of your contact page, context is the key of the variable you set the contact form to pick up in the Dynamic Value contact form tag field (i.e. CF7_GET key=’context’), and
more generally, to always pass the title of the post:
<?php global $post; echo '<a href="http://mysite.com/contact?context='.urlencode(get_the_title($post->ID)) .'">Contact Us</a>'; ?>
Now when a user clicks the link, they’ll be taken to your contact form with the context field pre-populated. Awesome!
Here’s an example, context set to “how-to-create-a-dynamic-wordpress-contact-form”:
Contact Me!
An Alternate Solution: Contact Form on each page
If you’d rather put the contact form on each page, you can do that too! You only need to create one form, but for your dynamic text field you just use the shortcode CF7_get_post_var key='title'
instead of the CF7_GET
shortcode.
Want to set the URL instead of the post title? Just use the shortcode CF7_URL
instead of CF7_get_post_var key=title
.
Thanx for this tutorial and for the plugin.
I just have one more question. Can “CF7_get_post_var” reference custom fields?
Hi Christian,
CF7_get_post_var can get any key in the $post object, but at this time it cannot get custom fields. This is on my to-do list for the next release. Of course, the great thing is that you can always write shortcodes to extend the plugin if you need 🙂
Chris
AWESOME WORK!!
I love your plugin and would like to know if the following is possible.
I am running a wordpress with buddypress site and would like to know how I can ‘call’ the users info such as name, surname, email and other fields so that the form will be autocompleted with the users info.
Thank you in advance.
Hi Maryke,
Glad you like the plugin. CF7 DTX can use the current user’s info to populate form. You can use the
CF7_get_current_user
shortcode.Where
att_name
is any attribute from here: http://codex.wordpress.org/Function_Reference/get_currentuserinfoFor example,
would give you the current user’s email address.
Hope that helps,
Chris
hey I just want to ask:
I want to make one text field below the radio button and it become requred text field when the “other” option have choosen. just like this form: http://www.coldfusionjedi.com/demos/jqueryvalidation/test4.html
As a marketing guy with just enough engineering chops to be dangerous, I think your plug-in may solve two challenges for us. Let me ask:
* We have email campaigns that we currently have to have a unique contact form (and page) if we want to track the campaign conversions (using unique subject line). We can use your plug in to pass a campaign identifier through on submission (not as unique subject line anymore, but that wouldn’t be necessary)
* We ask users to fill in a survey, but each set of survey responses from each user needs to be unique (we don’t need to track each user AND the unique response; we can match the unique response identifier after submission). We can use your plug in to pass a variable into a hidden field in the survey, which will be sent via email, as usual upon submission, correct?
Appreciate your insights. Thanks.
Yup! You can pass whatever variable(s) you want to the contact form, just pass it via GET or POST. When you retrieve it on the contact form end, you can place those variables in the subject or anywhere in the body, whichever works best. And yes, you could pass the values into hidden fields in the contact form as well 🙂
Rather then GET or POST is it possible to get a PHP Global-variable in a dynamic text field ?
I am using inline php-code with EXEC-PHP-plugin on the same page as CF7.
Thanks
Sure, you’d just have to write a custom shortcode that returns the variable and use it in the dynamic text field 🙂
Thanks Chris, that is actually what I tried.
My problem, not really related to your plugin then, is that my functions do not “see” (have access to) any global variable ?!
I am new to PHP but, yes I defined them as “global” in the function and also tried $GLOBALS[‘my_variable]; No dice.
Any suggestion would be highly appreciated. Thanks,
Make sure you’re doing it like this:
You could try it with the global $user_ID to test it out (assuming you’re logged in).
If it’s not working for the global variable you’ve set, it is likely that the global variable is not set prior to the shortcode running, so you get a null value – if you want to get the value, it needs to be set before the shortcode runs. Perhaps it would make sense to generate the variable within the shortcode, rather than separately on the page?
This is what i was looking for….. Great Plugin..
Hi!
I’ve done what you say here, but there’s still something missing. When I click at the button “Demana informació” (More information) the context file is getting the right text but it doesn’t give the spaces correctly. It outputs all text together. You can see it here
http://www.infordisa.com/formacio/cursos-per-treballadors
Do you know why is this happening?
Thanks for the answer and the plugin 😉