Shortcode for Contact Form 7

Shortcodes are widely used feature of WordPress. Many plugin and theme developers use this interesting feature to easily push custom contents inside posts or pages. But what if we could add a custom field within a Contact Form 7 (wpcf7) form? That’s exactly what I tried to do couple of days back and it was really exciting.

In case you don’t know what is wpcf7 or shortcode, please have a look at WordPress codex. Here I’ll explain how I have added the shortcode to the wpcf7 form and later fetched their values.

I have worked with WordPress 3.9.2 and Contact Form 7 v 3.8.1 to test these features.

There is the method wpcf7_add_shortcode which adds shortcode directly into contact form 7. Well while this function has its virtues, there are some disadvantages. Most of the time we cannot use the function right away because that will give us the “undefined function” error. We have to add a wrapper action to make sure that the wpcf7 plugin is loaded. Continue reading