JavaScript Integration

JavaScript is one of the most important scripts when it comes to WordPress. We thought about that and offered several objects through which you can access to geo information.

Within the first line of the HTML element, we have placed special objects through which you can access to geo information:

  • window.wp.geo – Using standard WordPress JavaScript wp object
  • window.cfgeo – Using direct window object
  • cf.geoplugin – Using old CF Geo Plugin cf object (included conflict fix from the version 6.x.x).

All of this objects are the same and return geo information like this:

{
	url: "https://yourdomain.com/",
	host: "cfgeoplugin.com",
	protocol: "https",
	ip: "217.138.192.36",
	ip_version: "4",
	ip_dns: "",
	ip_dns_host: "",
	ip_dns_provider: "",
	ip_number: "3649749028",
	country_code: "GB",
	country: "Great Britain",
	region: "England",
	region_code: "",
	city: "United Kingdom",
	continent: "Europe",
	continent_code: "EU",
	address: "United Kingdom, England, Great Britain",
	area_code: "",
	dma_code: "",
	latitude: "51.508529663086",
	longitude: "-0.12574000656605",
	timezone: "Europe/Zurich",
	locale: "en_GB,ga_GB,cy_GB,gd_GB,kw_GB",
	currency: "GBP",
	currency_symbol: "£",
	base_currency: "USD",
	base_currency_symbol: "$",
	currency_converter: "0.7684",
	ip_host: "176.9.103.101",
	timestamp: "1580845604",
	timestamp_readable: "Tue, 04 Feb 2020 19:46:52 +0000",
	current_time: "20:10:57",
	current_date: "February 4, 2020",
	version: "7.8.8",
	is_vat: "1",
	in_eu: "1",
	gps: "0",
	accuracy_radius: "200km",
	lookup: "unlimited",
	credit: "These geographic information provides <a href="http://cfgeoplugin.com/" target="_blank">CF Geo Plugin</a>",
	flag: "https://yourdomain.com/wp-content/plugins/cf-geoplugin/assets/flags/4x3/gb.svg",
}

Basic usage

When you have this informations, you can easy use it in your code. Here is the example:

(function(geo){
	var city = geo.city,
        state	= geo.state,
        country = geo.country,
        ip  = geo.ip;
		
	console.log(geo.country);
	
}(window.wp.geo || window.cfgeo || cf.geoplugin));

Also you can use it in the jQuery or Zepto:

(function($){

	$('input#adress').val(window.cfgeo.address);

}(jQuery || window.jQuery || Zepto || window.Zepto));

AJAX & CACHE

If you have problem with the cache, we provide AJAX action what you can use to get geo information from the plugin.

(function($){
	var  nonce = 'YOU MUST PROVIDE IT VIA PHP (READ BELOW)';
	$.ajax({
		method : 'POST',
		data: {
			'cfgeo_nonce' : nonce
		},
		cache : false,
		async : false,
		url: ajaxurl + '?action=cfgeo_cache',
	}).done( function( data ) {
		console.log( data );  
	});
}(jQuery || window.jQuery || Zepto || window.Zepto));

You need to generate nonce via PHP and provide to AJAX:

wp_create_nonce('cfgeo-process-cache-ajax');

You can do with hidden input, data attribute, inline coding, wp_localize_script() or some 3rd way.

If this nonce missing you will not be able to get geo informations.