Obtaining Location Details From Email Forms Using CF7

Posted By : Aparajita Singh | 30-Jun-2019

In this blog post, I'm going to explain how to get country, state and city of location in given mail id with the help of CF7(Contact form 7).

Step 1:- create page location.php and include this page in function.php

             geoplugin_countryName != null){
    $locationofcountry=$query->geoplugin_countryName;
    // Set the cookie
  } else {
    $locationofcountry= 'Unable to get Location of Country';
  }
  setcookie('visit_location',  $locationofcountry);
  return $form_tags;
}
add_action('init', 'getLocationofcountry', 1);
    
//   /*Calling the function from contact-form-7 module and passing the result of the function stylus_ip_location_get_cc */
function getcitylocation( $form_tags ){
  if($_COOKIE['visit_city']){
  $$citylocation = $_COOKIE['visit_city'];
  } else {
  $$citylocation= 'Unable to get City';
  }
  if ($form_tags['name'] == 'city' ) {
  $form_tags['values'][] = 'City: ' .$citylocation; 
 }
 return $form_tags;
 }
 add_filter( 'wpcf7_form_tag', 'getcitylocation' );
function getLocationofcity($form_tags){
  $ip = $_SERVER['HTTP_HOST'];
   $query = @json_decode(file_get_contents("http://www.geoplugin.net/json.gp?ip=".$ip));
  if($query && $query->geoplugin_countryName != null){
    $citylocation= $query->geoplugin_city;
    // Set the cookie
  } else {
  $citylocation= 'Unable to get City Location';
  }
  setcookie('visit_city',  $citylocation);
  return $form_tags;
}
add_action('init', 'getLocationofcity', 1);
//   /*Calling the function from contact-form-7 module and passing the result of the function stylus_ip_location_get_cc */
function getstatelocation( $form_tags ){
  if($_COOKIE['visit_state']){
  $statelocation = $_COOKIE['visit_statelocation'];
  } else {
  $statelocation= 'Unable to get State Location';
  }
  if ($form_tags['name'] == 'state' ) {
  $form_tags['values'][] = 'State: ' .$statelocation; 
 }
 return $form_tags;
 }
 add_filter( 'wpcf7_form_tag', 'getstatelocation' );
function getLocation($form_tags){
  $ip = $_SERVER['HTTP_HOST'];
   $query = @json_decode(file_get_contents("http://www.geoplugin.net/json.gp?ip=".$ip));
  if($query && $query->geoplugin_countryName != null){
    $statelocation= $query->geoplugin_regionName;
    // Set the cookie
  } else {
    $statelocation= 'Unable to get State';
  }
  setcookie('visit_state',  $statelocation);
  return $form_tags;
}
add_action('init', 'getLocation', 1);   
        

Step 2:- Add this code [state class:state] in form tag of CF7 and by using class hide input field. same as [city class:city] and [country class:country].

Step3:- Add shortcode in mail tag.Here is shortcode for State:- [state] Country:- [country] City:- [city],after adding all these shortcode in mail tag we can get state,city,country of location in given mail id. Hope you understand how to get country,state and city of location in given mail id with the help of CF7(Contact form 7).

About Author

Author Image
Aparajita Singh

She is having experiance in wordpress development and she would like to learn new technologies.

Request for Proposal

Name is required

Comment is required

Sending message..