How to display location of an user in a mail send using Contact Form 7 plugin by passing the IP address in a Wordpress Site?

By | 03rd Feb 2015 | 4 min read

In wordpress site one of the most commonly used plugin is Contact Form 7. In this plugin one of the main feature is that when a user submits a form we could get IP address of the user, but unfortunately we can't get the location. Here I will be explaining how we can create a custom plugin to get the location by using corresponding IP address and a third party service. You can also refer the article How to get the IP address and Country code using the API key of a third party service?

You download and enable Contact Form 7. In contact form 7 plugin there are a few custom special tags defined. The use of this special tags are they automatically generate values and can be passed in form automatically. These values are called in file 'special-mail-tags.php'. These are the list of special tags defined in this module.

  • wpcf7.remote_ip->To get the IP Address of the User.
  • wpcf7.url->To get the URL of the page where the request has been send
  • wpcf7.date->To get the form submit date
  • wpcf7.time->To get the form submit time

Next doubt that comes to your mind is how to pass this value in mail. Goto the page contact form edit 'wp-admin/admin.php?page=wpcf7' there in field mail, there is a Message body region in that you can call the special tags as follows [wpcf7.remote_ip].

Hope you have got a brief idea about Contact Form 7 plugin. Now my requirement here is that when the user receives the mail he should get the location of person who submits the form. So my task is to find the location from the IP address. Its not good practice to edit the the contributed plugin to achieve this. For this created a custom plugin location 'wp-content/plugins/custom-plugin'

In-order to get the location of an user from Ip address we have to integrate a third party configuration. Don't worry its not that difficult.The third party integration we are using here is IPInfoDB.

http://ipinfodb.com/ip_location_api.php

Our first step is to create an API key. To get an API key we have to register in the site IPInfoDB.

Once API key is ready we have to download the file ip2locationlite.class.php from the site IPInfoDB.

Next step is to create our custom plugin.I named it as "country_custom_plugin.php". Its always good to create a custom plugin inside a folder, so that all the required files for the corresponding plugin stays in the folder. Named the folder as "country_custom_plugin"

Move the file "ip2locationlite.class.php" to the folder "country_custom_plugin".

Now comes the code part. What I have done is here I found out tag used to call the special tags in Contact Form 7, tag is "wpcf7_special_mail_tags". Using this tag I called the my custom function using add_filter. Values passed in add_filter function are

  • $tag -> wpcf7_special_mail_tags -> The name of the Filter used in contact form to call special tags.
  • $function_to_add -> country_custom_ip_location -> Custom function name to get IP location
  • $priority -> 10 -> By default its 10
  • $accepted_args -> 2 -> Number of arguments the function accepts.

In my Mail body part I am passing the tag to get IP Location as [wpcf7.custom_ip_location]



/*Calling the function from contact-form-7 module and passing the result of the function stylus_ip_location_get_cc */

add_filter( 'wpcf7_special_mail_tags', 'country_custom_ip_location', 10, 2 );

/*Function to get location of an user from the ip address*/

function country_custom_ip_location( $output, $name ){
  /*including the third party integration to get IP Location*/
  include_once('ip2locationlite.class.php');

  /*Special tag values are passed in format wpcf7.$name which we convert to _$name*/
  $name = preg_replace( '/^wpcf7\./', '_', $name );

  /*If location is requested in contact form enter the loop*/
  if ( '_custom_ip_location' == $name ) {
    $ipLite = new ip2location_lite;
    
    /*Entering the API key value generated*/
    $ipLite->setKey('"Enter your API Key Here"');
    
    /*Getting the IP address*/
	  $ipaddress = preg_replace( '/[^0-9a-f.:, ]/', '', $_SERVER['REMOTE_ADDR'] );
	  
    /*Getting the Location*/
    $visitorGeolocation = $ipLite->getCity($ipaddress);

    if (!empty($visitorGeolocation) && is_array($visitorGeolocation)) {
       $output = $visitorGeolocation['regionName'] . ', ' . $visitorGeolocation['countryName'] . ', ' . $visitorGeolocation['countryCode'];
    }
  }
  return $output;
}

Here we return the values Region, Country name and Country Code. Now now you can try this out and if you have any doubt get back to us via comment.

Reference URL : http://ipinfodb.com/ip_location_api.php