How to Create a Dynamic WordPress Contact Form

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.

Make this field uneditablechecked
Keeps the user from changing this on you
Dynamic ValueCF7_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'" ]

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:


echo '<a href="'. 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:


global $post;
echo '<a href="'.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.

14 thoughts on “How to Create a Dynamic WordPress Contact Form

    • 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 🙂


  1. 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.

  2. 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 🙂

  3. 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 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:

          global $myVar;
          return $myVar;

          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?

Comments are closed.