Adding Visitor IP and Current Page to “Contact Form 7″ WordPress Plugin
I’ve loved using the Contact Form 7 WordPress Plugin so far, but it was missing two VITAL features:
I was unable to send the visitors IP address with the form submission, and I was unable to tell which exact page the form submission came from if I was using the form on more than one page.
Here is a fix for both of those little short falls:
Edit the plugin – You can do this through the wordpress plugin editor http://YOUR-DOMAIN-NAME/wp-admin/plugin-editor.php
Paste the following code just above the last line of code – The last line of code is simply “?>”
function curPageURL() {
$pageURL = ‘http’;
if ($_SERVER["HTTPS"] == “on”) {$pageURL .= “s”;}
$pageURL .= “://”;
if ($_SERVER["SERVER_PORT"] != “80″) {
$pageURL .= $_SERVER["SERVER_NAME"].”:”.$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return $pageURL;
}
Next search for this line of code which should appear directly in the middle of the plugin:
$form .= ‘<div style=”display: none;”>’;
Below this line, paste the following code.
// Addition By: Vince Samios, http://vincesamios.com
$curpageurl = curpageurl();
$visip=$_SERVER['REMOTE_ADDR'];
$form .= ‘<input type=”hidden” name=”page-url” value=”‘ . $curpageurl . ‘” />’;
$form .= ‘<input type=”hidden” name=”visitor-ip” value=”‘ . $visip . ‘” />’;
Now you can reference these values in the email like you would any other form field. Example below
Page: [page-url]
IP: [visitor-ip]
Another useful piece of code by “Travis” is for adding the contact form in PHP – this is useful if you would like to include it in the theme.
echo apply_filters(’the_content’,’
‘);
Update:
Added time and date of submission – Add this in the same place as the Visitor IP and Current Page additions.
date_default_timezone_set(‘GMT’);
$timedate = date(‘l jS \of F Y h:i:s A’);
$form .= ‘<input type=”hidden” name=”time-date” value=”‘ . $timedate . ‘” />’;
You can reference the time and date in the contact enquiry email by using:
[time-date]
Thanks for the snippet Vince,
I was searching your (blog) site for a contact form. I’m looking for an example, but I don’t see any.
I’m not using contact form 7 on my blog, just on various money sites. Since posting, I’ve also added a time stamp to contact form 7 so I can see the exact time and date of submission.
darn. i definitely want these features (IP & time stamp) & followed your instructions. but i just couldn’t get this code to work. plug-in would deactivate once the code was inserted. tried it by deactivating the plug-in first & then re-activating it. no dice. kept getting this error message:
Parse error: syntax error, unexpected T_STRING in /–/–/–/wp-content/plugins/contact-form-7/wp-contact-form-7.php on line 919
and
Parse error: syntax error, unexpected ‘:’ in /–/–/–/wp-content/plugins/contact-form-7/wp-contact-form-7.php on line 1810
any tips on getting it to work would be most appreciated. thanks!
Hmmm… probably a problem with copy/pasting the code…
Try pasting the code into notepad, the re-copy pasting it into the plugin. This sometimes helps sanitize the text formatting (but come to think of it, sounds unlikely in this case)
Also ensure you are pasting it in at the correct spot.
You don’t need to de-activate the plugin to add the code.
I’m using version 2.2.6 and “$form .= ‘’;” is no where to be found on the code.
Pingback: Contact Form 7 IP Address
You can also add this into the message body part of Contact form 7 and it will send the ip address.
[wpcf7.remote_ip]
It’s probably a new feature since this article was written but this article still ranks for the subject in google so it would be good for people to know.
I got this info from the wordpress site.
This just isn’t working for me. Which .php file in the plugin are we editing here?
I am going to adapt the code to work with the current version, you can check it out at my site