Create a shortcode for a dynamic link to a webform using Contact Form 7

I am working on a project that requires me to create product links that open an email form that has the product information pre-populated in the form. I came up with a simple solution that uses a couple WordPress plugins and shortcodes.

 

Here is the problem, a series of products in a shopping cart that, rather than having a ‘buy now’ button, they have a button that takes the customer to an email form that has the product information in it.

 

  1. Install the plugin Contact Form 7 and the extension to that plugin Contact Form 7 – Dynamic Text Extension.
  2. Create a new page for the contact form.
  3. Create a form using the Contact Form 7 tag editor and add a dynamic field with the following parameters:
    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.
  4. Take the short code that is generated and put it into the contact form page you made in step 2.
  5. Following the instructions on the CF7 DXE page, create the PHP link.
  6. Now to turn it into a shortcode you have to alter that link some. What is suggested looks like this:

    < ?php global $post;
    echo 'Contact Us';?>
  7. To create a shortcode you open your function.php file in your child theme and create a function. You can call the function anything that makes sense to you but it should look like this:function contact_form()
  8. Following that you create a curly bracket around the call ‘return’ and the html part of the above code so now it should look like this:

    function contact_form()
    {return Contact Us';
    }
  9. You end by creating the shortcode like this:add_shortcode('TheNameYouWantToType', 'yourfunctionname');
    (yourfunctionname in this case is contact_form, TheNameYouWantToType is what goes in the shortcode)
  10. the final code looks like this:
    function contact_form(){
    return Contact Us';
    }
    add_shortcode('TheNameYouWantToType', 'yourfunctionname');

And that is it. Now you can use the short code that you named anywhere in your posts and it will create a link to the form with the post title in a field of the email. I added a class to a <div> around the text so that I could style the button.

 

Was this useful to you? Please give feedback or ask questions in the comment section bellow