Allows you to insert all or part of the global GeoNames database in your WordPress base.
This lightweight plugin makes it easy to install the millions of GEONAMES Data on your WordPress site.
It allows :
A shortcode is also available to create a city region and country taxonomy.
This plugin will give you plenty of ideas to improve the quality of your website.
Official GeoNames website.
wp-geonames
folder and its contents into the wp-content/plugins/
directory of your WordPress installationYou can insert as many file as you want.
You must use the WordPress tools to get the database. WPDB is your friend.
You can write the code directly in your template or in functions.php of your theme.
Name of the table : ($wpdb->prefix)geonames
Names of the columns :
idwpgn
(bigint)geonameid
(bigint)name
(varchar)asciiname
(varchar)alternatenames
(text)latitude
(decimal)longitude
(decimal)feature_class
(char)feature_code
(varchar)country_code
(varchar)cc2
(varchar)admin1_code
(varchar)admin2_code
(varchar)admin3_code
(varchar)admin4_code
(varchar)population
(bigint)elevation
(int)dem
(smallint)timezone
(varchar)modification_date
(date)Feature Class & Code here.
Example : get GPS position for a specific city in a specific country :
global $wpdb; $s = $wpdb->get_row("SELECT latitude, longitude FROM ".$wpdb->prefix."geonames WHERE name='Paris' and country_code='FR' "); echo $s->latitude . " - " . $s->longitude;
Example : 10 most populous cities in Switzerland :
global $wpdb; $s = $wpdb->get_results("SELECT name, population FROM ".$wpdb->prefix."geonames WHERE country_code='CH' and feature_class='P' ORDER BY population DESC LIMIT 10"); foreach($s as $t) { echo $t->name. " : " . $t->population . "<br />"; }
Example : hotels within 40 km from Marbella (ES) :
global $wpdb; $p = $wpdb->get_row("SELECT latitude, longitude FROM ".$wpdb->prefix."geonames WHERE name='Marbella' and country_code='ES' "); $dlat = 40 / 1.852 / 60; $dlon = 40 / 1.852 / 60 / cos($p->latitude * 0.0174533); $s = $wpdb->get_results("SELECT name, latitude, longitude FROM ".$wpdb->prefix."geonames WHERE country_code='ES' and feature_code='HTL' and latitude<".($p->latitude+$dlat)." and latitude>".($p->latitude-$dlat)." and longitude<".($p->longitude+$dlon)." and longitude>".($p->longitude-$dlon)." LIMIT 100"); foreach($s as $t) { $d = (floor(sqrt(pow(($p->latitude-$t->latitude)*60*1.852,2)+pow(($p->longitude-$t->longitude)*60*1.852*cos($p->latitude * 0.0174533),2)))); if($d<=40) echo $t->name. " : " . $d . " km<br />"; }
Example : Suggest cities during the typing by the user (like Google search)
You must use Ajax action and PHP function with the name “wpgeonamesAjax”
In your theme, in function.php ; add :
function wpgeonamesAjax() {
global $wpdb;
$s = $wpdb->get_results(“SELECT name
FROM “.$wpdb->prefix.”geonames
WHERE country_code=’FR’ and feature_class=’P’ and name LIKE ‘”.strip_tags($_POST[“city”]).”%’
ORDER BY name
LIMIT 10″);
foreach($s as $t) {
echo ‘
‘.$t->name.’
‘;
}
}
In your theme, in the right page ; add :
<input id="inpCity" name="inpCity" type="text" onkeyup="sugg(this,'<?php echo admin_url('admin-ajax.php'); ?>');" /> <div class="suggCity" id="suggCity"></div> <script> function sugg(f,g){ jQuery(document).ready(function(){ jQuery.post(g,{'action':'wpgeonamesAjax','city':f.value},function(r){ jQuery('#suggCity').empty();jQuery('#suggCity').append(r.substring(0,r.length-1)); }); }); } </script>
08/02/2022
21/03/2020
14/03/2019 – Add Postal code database.
27/01/2018 – Curl used by default if exists in place of File_Get_Content.
12/01/2018 – Add tab to edit and change datas.
24/06/2017
09/10/2015 – Fix “Fatal error: Out of memory (allocated xxx) (tried to allocate xxx bytes)”.
07/06/2016 – 1.3.1 – Fix error when reactivate (header already sent…).
06/08/2015 – Fix installation bug.
01/12/2014 – Add Ajax hook.
25/11/2014 – First stable version.