Howdy! Thanks for coming and enjoy your stay! Take a look at the sitemap and don't forget to chirp!

WordPress: Extending the Contact Form 7 Plugin

February 1st, 2010

There are tonnes of good plugins for displaying contact forms on your WordPress blog, even a simple comment form without displaying the comments would do just fine, but let’s speak about one called Contact Form 7 written by Takayuki Miyoshi. I don’t think there’s a reason behind the number 7, perhaps it meant the year 2007, when Takayuki published the first version of his plugin.

Adding the Source URL Module to Contact Form 7

Adding the Source URL Module to Contact Form 7

First of all I’d like to note that he plugin is very well written, it’s very, and I mean VERY flexible, and only due to its flexibility we’re allowed to extend its functionaltiy. Of course Takayuki’s probably missed some filters and hooks that other plugin and theme developers would love to see, but the modules directory is good enough, despiting the fact that it’ll probably be erased and rewritten after an ongoing plugin update.

Now, suppose you have designed a placed a contact form in your sidebar as a widget, using the Contact Form 7 shortcode. It’s got all the necessary fields, AJAX powered form, Akismet spam check & so on. But we’re missing something. Contact Form 7 is located in your sidebar, which means that you’re displaying it on most (if not all) of your posts and pages. Now when somebody writes a message via your contact form, you’re left guessing which page the visitor was viewing while typing.

This may be crucial in e-commerce situations, say you’re selling books from your blog, and a visitor contacts you to ask “who’s the publisher of this book?” or “how many pages does this book have?”. So you received an e-mail and you have no idea which book is being mentioned. You’re lucky if you only work with one publisher, or all your books are 100 pages long ;)

Anyways, let’s create a new file in the modules folder of the plugin, and call it sourceurl.php:

wpcf7_add_shortcode('sourceurl', 'wpcf7_sourceurl_shortcode_handler', true);

function wpcf7_sourceurl_shortcode_handler($tag) {
	if (!is_array($tag)) return '';

	$name = $tag['name'];
	if (empty($name)) return '';

	$html = '<input type="hidden" name="' . $name . '" value="http://' . $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"] . '" />';
	return $html;
}

This is very easy to understand once you’ve read (and understood) what’s written in the other modules. Try to take a look at text.php for a while, it includes the code for both text and e-mail fields. I copied some of the code for the text fields, simplified it a little and removed the validation process (why validate our own URL?), and voila, 12 lines of code ;)

Save the file and go to your Contact Form 7 settings in your WordPress admin panel. Select the contact form you’d like to add source URL to and in the part where it says “Form” add the new short code (sourceurl) and give it a name (thesource):

[sourceurl thesource]

Now you can use “thesource” in your Mail part of the contact form settings, say:

Source URL: <a href="[thesource]">[thesource]</a><br />

Then look for the new Source URL in your e-mail ;) So if you’ve used more than one contact form on several different pages, just to distinguish them in your emails, you can merge them into one now. You can use a similar way to add other interesting data to your e-mails such as, say the user’s IP address for instance, browser capabilities, etc.

Related posts:

  1. Extending the jQuery Lightbox Plugin: Custom Link
  2. Extending Custom Post Types in WordPress 3.0
  3. WordPress: Twitter Friendly Links Plugin. Stage Two
  4. Plugin Development: Technical Support for WordPress
  5. Give WordPress the iPhone Look: wpTouch Plugin

23 Responses to “WordPress: Extending the Contact Form 7 Plugin”

  1. Sorry, WordPress filtered the input field:

    $html = ‘*input type=”hidden” name=”‘ . $name . ‘” value=”‘ . $_SERVER['REMOTE_ADDR'] . ‘” /*’;

    ATTENTION: Remove * and use the appropriate html (I only did this so that wordpress wouldn’t filter it again)

    Cheers Kovshenin and thanks again ;)

Including pingbacks & trackbacks

  1. uberVU - social comments linked here on February 1, 2010 at 8:54 pm