{"id":87,"date":"2004-08-27T20:22:46","date_gmt":"2004-08-27T18:22:46","guid":{"rendered":"http:\/\/frenchfragfactory.net\/ozh\/archives\/2004\/08\/27\/ip-to-nation-plugin\/"},"modified":"2010-01-25T22:03:08","modified_gmt":"2010-01-25T20:03:08","slug":"ip-to-nation-plugin","status":"publish","type":"post","link":"https:\/\/planetozh.com\/blog\/2004\/08\/ip-to-nation-plugin\/","title":{"rendered":"IP to Nation WordPress Plugin"},"content":{"rendered":"<p>There it is, my very first real plugin for WordPress (&quot;very first&quot; meaning of course you can expect two or three more within the next 200 years). This plugin guesses your visitor&#39;s Country from his IP (check these <a href=\"\/projects\/ip2nation.php\">examples<\/a>). Based upon the very good <a href=\"http:\/\/www.ip2nation.com\/\">ip2nation<\/a> free data, I&#39;ve simply packaged it as a plugin.<br \/>\n<!--more--><\/p>\n<h2>Set up the MySQL table<\/h2>\n<p>First of all, go to <a href=\"http:\/\/www.ip2nation.com\/\">ip2nation<\/a> and download the .sql file (I&#39;m not mirroring it because it is updated from times to times, so get the latest one) This .sql file will create two tables, <em>ip2nation <\/em>and <em>ip2nationCountries<\/em>, containing top secret data you don&#39;t want explanations about :)<\/p>\n<p><strong>Newbie tips<\/strong> : first, install <a href=\"http:\/\/www.phpmyadmin.net\/\">PHPMyAdmin<\/a> on your website. This is a great interface for anything you can do with MySQL databases. Once it&#39;s done, look for the &quot;SQL&quot; link in the menu : you will be prompted for a .sql file, for example the one you just downloaded on your computer. It will set up and populate the required tables.<\/p>\n<h2>Download the plugin<\/h2>\n<div class=\"download\">\nDownload the plugin :<br \/>\n<a href=\"http:\/\/downloads.wordpress.org\/plugin\/ozhs-ip-to-nation.zip\">ozhs-ip-to-nation.zip<\/a><br \/>\nExtract and upload to your blog, preserving directory structure if any.<br \/>\n<small>Note: download counter here and stats on wordpress.org may differ and reflect the number of downloads before this plugin was hosted on the plugin directory<\/small>\n<\/div>\n<h2>Use the plugin<\/h2>\n<p>I&#39;ve created two &quot;template tags&quot; you can use in your pages :<\/p>\n<ul>\n<li><strong>wp_ozh_getCountryName()<\/strong> : prints the country name (France, Belgium, Vanuatu, stuff like this).<\/li>\n<li><strong>wp_ozh_getCountryCode()<\/strong> : prints the country &quot;code&quot; : 2 letters, as in top level domains (fr, be, it, stuff like this)<\/li>\n<\/ul>\n<p>These two functions have to optional arguments :<\/p>\n<ul>\n<li><strong>$display <\/strong>: defaults to 0 (zero), if you want to print the result or just return it (see examples below)<\/li>\n<li><strong>$ip <\/strong>: defaults to $_SERVER[&#39;REMOTE_ADDR&#39;], which is the visitor&#39;s IP address.<\/li>\n<\/ul>\n<p>(Note : If you make a call to just one or to both template tags, it will cost you only 1 SQL query.)<\/p>\n<p>(Note 2 : I named the plugin file and the function with a beginning &quot;wp_ozh_&quot; to prevent any duplicate function name, if someone ever creates another similar plugin)<\/p>\n<h2>Examples of use<\/h2>\n<p>For example, you could use the plugin with the following code :<\/p>\n<div class=\"igsh-code-box\" id=\"ig-sh-1\"><pre class=\"language-php line-numbers\" data-no-optimize=\"1\" data-cfasync=\"false\"><code class=\"language-php\">&lt;?php\r\necho &quot;You are probably from &quot;. wp_ozh_getCountryName() .&quot;&lt;br \/&gt;&quot;;\r\necho &quot;If so, your country flag is  &lt;img alt=\\&quot;your flag\\&quot;    \r\n   src=\\&quot;\/images\/flags\/flag_&quot;\r\n  . wp_ozh_getCountryCode() . &quot;.gif\\&quot;&gt;&lt;br \/&gt;&quot;;\r\n?&gt;<\/code><\/pre><\/div>\n<p>The template <em>wp_ozh_getCountryCode()<\/em> goes well with this <a href=\"http:\/\/planetozh.com\/download\/flags.zip\">awesome tiny flags pack<\/a> you&#39;ve seen everywhere on the web (which were originally created by a guy I know, Zarkof, and are free to use, which is a nice gift considering the amount of pixel skills and time he must have put in these 175 flags :)<\/p>\n<p>Passing the parameter &quot;0&quot; (zero) to a function could be useful for example to test a language redirection without printing anything. The following example determines wether the user is supposedly speaking French or not :<\/p>\n<div class=\"igsh-code-box\" id=\"ig-sh-2\"><pre class=\"language-php line-numbers\" data-no-optimize=\"1\" data-cfasync=\"false\"><code class=\"language-php\">&lt;?php\r\n$country = wp_ozh_getCountryName(0) ;\r\nswitch ($country) {\r\n   case &quot;France&quot; :\r\n   case &quot;Guadeloupe&quot; :\r\n   case &quot;Luxembourg&quot; :\r\n   case &quot;Monaco&quot; :\r\n   case &quot;Martinique&quot; :\r\n   case &quot;New Caledonia&quot; :\r\n   case &quot;French Polynesia&quot; :\r\n   case &quot;St. Pierre and Miquelon&quot; :\r\n   case &quot;Reunion&quot; :\r\n   case &quot;French Southern Territories&quot; :\r\n   case &quot;Wallis and Futuna Islands&quot; :\r\n        echo &quot;you speak French&quot;;\r\n        break;\r\n   default :\r\n        echo &quot;you may not speak French&quot;;\r\n}\r\n?&gt;<\/code><\/pre><\/div>\n<p>You can also pass an IP address to the functions, which could be used to print your commenters&#39; country : in <em>blogroot\/wp-comments.php<\/em> (WP 1.2) or <em>blogroot\/wp-content\/themes\/yourtheme\/comments.php<\/em> (WP 1.5+), where you see <em>&lt;?php comment_author_link() ?&gt;<\/em>, put the following :<\/p>\n<div class=\"igsh-code-box\" id=\"ig-sh-3\"><pre class=\"language-php line-numbers\" data-no-optimize=\"1\" data-cfasync=\"false\"><code class=\"language-php\">&lt;?php comment_author_link() ?&gt; from\r\n &lt;?php wp_ozh_getCountryName(1,$comment-&gt;;comment_author_IP) ?&gt;<\/code><\/pre><\/div>\n<p>This will output something like : <em>JohnDoe from India<\/em><\/p>\n<p>To put a tiny flag by the name (like in comments here), you could use the following code :<\/p>\n<div class=\"igsh-code-box\" id=\"ig-sh-4\"><pre class=\"language-php line-numbers\" data-no-optimize=\"1\" data-cfasync=\"false\"><code class=\"language-php\">&lt;?php\r\necho &#039;&lt;img alt=&quot;your flag&quot; src=&quot;\/images\/flags\/flag_&#039;\r\n     . wp_ozh_getCountryCode(0,$comment-&gt;comment_author_IP)\r\n     . &#039;.gif&quot;&gt;&#039; ;\r\n?&gt;<\/code><\/pre><\/div>\n<h2>Disclaimer<\/h2>\n<p>I am not positive that the ip2nation data produce a 100% correct result. You may happen to get generic or incorrect results, such as &quot;Europe&quot; or &quot;US Educationnal&quot; instead of a country. You might tell the author of the data, I&#39;m just a packager :)<\/p>\n<h2>Sandbox, testing and debugging help<\/h2>\n<p>Please, <strong>don&#39;t post a comment<\/strong> here just to check what flag will come up for you. To do so, please have a look at the <a href=\"\/projects\/ip2nation.php\">examples and demo page<\/a>, as stated in the very first line of this article. Test comments will be removed and their author won&#39;t start with a good karma if they ask for my support afterwards.<\/p>\n<p>Please, <strong>be accurate and give details<\/strong> when you&#39;re asking for support. &quot;The plugin doesn&#39;t work, can you help ?&quot; is completely useless yet very frequent. State the PHP code you use and the error or unexpected output it generates.<\/p>\n<p>Please, <strong>don&#39;t post multiple lines of code<\/strong> here when you&#39;re asking for support or debugging help. Upload your code somewhere I can check it, or use <a href=\"http:\/\/www.rafb.net\/paste\/\">this site<\/a> to paste your code and link it here.<\/p>\n<p>And please, please, please &#8230; <strong>read the instructions and examples<\/strong> here before asking something that is already answered and explained here.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Wordpress plugin : IP to Nation<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[21],"tags":[2,10,85,245],"class_list":["post-87","post","type-post","status-publish","format-standard","hentry","category-published","tag-code","tag-php","tag-plugins","tag-wordpress"],"_links":{"self":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/posts\/87","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/comments?post=87"}],"version-history":[{"count":0,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/posts\/87\/revisions"}],"wp:attachment":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/media?parent=87"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/categories?post=87"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/tags?post=87"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}