{"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 id=\"ig-sh-1\" class=\"syntax_hilite\">\n\n\t\t<div class=\"toolbar\">\n\n\t\t<div class=\"view-different-container\">\n\t\t\t\t\t\t<a href=\"#\" class=\"view-different\">&lt; View <span>plain text<\/span> &gt;<\/a>\n\t\t\t\t\t<\/div>\n\n\t\t<div class=\"language-name\">php<\/div>\n\n\t\t\n\t\t<br clear=\"both\">\n\n\t<\/div>\n\t\n\t<div class=\"code\">\n\t\t<ol class=\"php\" style=\"font-family:monospace\"><li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #000000;font-weight: bold\">&lt;?php<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #b1b100\">echo<\/span> <span style=\"color: #0000ff\">&quot;You are probably from &quot;<\/span><span style=\"color: #339933\">.<\/span> wp_ozh_getCountryName<span style=\"color: #009900\">&#040;<\/span><span style=\"color: #009900\">&#041;<\/span> <span style=\"color: #339933\">.<\/span><span style=\"color: #0000ff\">&quot;&lt;br \/&gt;&quot;<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #b1b100\">echo<\/span> <span style=\"color: #0000ff\">&quot;If so, your country flag is &nbsp;&lt;img alt=<span style=\"color: #000099;font-weight: bold\">\\&quot;<\/span>your flag<span style=\"color: #000099;font-weight: bold\">\\&quot;<\/span> &nbsp; &nbsp;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #0000ff\"> &nbsp; src=<span style=\"color: #000099;font-weight: bold\">\\&quot;<\/span>\/images\/flags\/flag_&quot;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; <span style=\"color: #339933\">.<\/span> wp_ozh_getCountryCode<span style=\"color: #009900\">&#040;<\/span><span style=\"color: #009900\">&#041;<\/span> <span style=\"color: #339933\">.<\/span> <span style=\"color: #0000ff\">&quot;.gif<span style=\"color: #000099;font-weight: bold\">\\&quot;<\/span>&gt;&lt;br \/&gt;&quot;<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #000000;font-weight: bold\">?&gt;<\/span><\/div><\/li>\n<\/ol>\t<\/div>\n\n<\/div>\n\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 id=\"ig-sh-2\" class=\"syntax_hilite\">\n\n\t\t<div class=\"toolbar\">\n\n\t\t<div class=\"view-different-container\">\n\t\t\t\t\t\t<a href=\"#\" class=\"view-different\">&lt; View <span>plain text<\/span> &gt;<\/a>\n\t\t\t\t\t<\/div>\n\n\t\t<div class=\"language-name\">php<\/div>\n\n\t\t\n\t\t<br clear=\"both\">\n\n\t<\/div>\n\t\n\t<div class=\"code\">\n\t\t<ol class=\"php\" style=\"font-family:monospace\"><li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #000000;font-weight: bold\">&lt;?php<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #000088\">$country<\/span> <span style=\"color: #339933\">=<\/span> wp_ozh_getCountryName<span style=\"color: #009900\">&#040;<\/span><span style=\"color: #cc66cc\">0<\/span><span style=\"color: #009900\">&#041;<\/span> <span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #b1b100\">switch<\/span> <span style=\"color: #009900\">&#040;<\/span><span style=\"color: #000088\">$country<\/span><span style=\"color: #009900\">&#041;<\/span> <span style=\"color: #009900\">&#123;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp;<span style=\"color: #b1b100\">case<\/span> <span style=\"color: #0000ff\">&quot;France&quot;<\/span> <span style=\"color: #339933\">:<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp;<span style=\"color: #b1b100\">case<\/span> <span style=\"color: #0000ff\">&quot;Guadeloupe&quot;<\/span> <span style=\"color: #339933\">:<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp;<span style=\"color: #b1b100\">case<\/span> <span style=\"color: #0000ff\">&quot;Luxembourg&quot;<\/span> <span style=\"color: #339933\">:<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp;<span style=\"color: #b1b100\">case<\/span> <span style=\"color: #0000ff\">&quot;Monaco&quot;<\/span> <span style=\"color: #339933\">:<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp;<span style=\"color: #b1b100\">case<\/span> <span style=\"color: #0000ff\">&quot;Martinique&quot;<\/span> <span style=\"color: #339933\">:<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp;<span style=\"color: #b1b100\">case<\/span> <span style=\"color: #0000ff\">&quot;New Caledonia&quot;<\/span> <span style=\"color: #339933\">:<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp;<span style=\"color: #b1b100\">case<\/span> <span style=\"color: #0000ff\">&quot;French Polynesia&quot;<\/span> <span style=\"color: #339933\">:<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp;<span style=\"color: #b1b100\">case<\/span> <span style=\"color: #0000ff\">&quot;St. Pierre and Miquelon&quot;<\/span> <span style=\"color: #339933\">:<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp;<span style=\"color: #b1b100\">case<\/span> <span style=\"color: #0000ff\">&quot;Reunion&quot;<\/span> <span style=\"color: #339933\">:<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp;<span style=\"color: #b1b100\">case<\/span> <span style=\"color: #0000ff\">&quot;French Southern Territories&quot;<\/span> <span style=\"color: #339933\">:<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp;<span style=\"color: #b1b100\">case<\/span> <span style=\"color: #0000ff\">&quot;Wallis and Futuna Islands&quot;<\/span> <span style=\"color: #339933\">:<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; &nbsp; &nbsp; <span style=\"color: #b1b100\">echo<\/span> <span style=\"color: #0000ff\">&quot;you speak French&quot;<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; &nbsp; &nbsp; <span style=\"color: #b1b100\">break<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp;<span style=\"color: #b1b100\">default<\/span> <span style=\"color: #339933\">:<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; &nbsp; &nbsp; <span style=\"color: #b1b100\">echo<\/span> <span style=\"color: #0000ff\">&quot;you may not speak French&quot;<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #009900\">&#125;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #000000;font-weight: bold\">?&gt;<\/span><\/div><\/li>\n<\/ol>\t<\/div>\n\n<\/div>\n\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 id=\"ig-sh-3\" class=\"syntax_hilite\">\n\n\t\t<div class=\"toolbar\">\n\n\t\t<div class=\"view-different-container\">\n\t\t\t\t\t\t<a href=\"#\" class=\"view-different\">&lt; View <span>plain text<\/span> &gt;<\/a>\n\t\t\t\t\t<\/div>\n\n\t\t<div class=\"language-name\">php<\/div>\n\n\t\t\n\t\t<br clear=\"both\">\n\n\t<\/div>\n\t\n\t<div class=\"code\">\n\t\t<ol class=\"php\" style=\"font-family:monospace\"><li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #000000;font-weight: bold\">&lt;?php<\/span> comment_author_link<span style=\"color: #009900\">&#040;<\/span><span style=\"color: #009900\">&#041;<\/span> <span style=\"color: #000000;font-weight: bold\">?&gt;<\/span> from<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp;<span style=\"color: #000000;font-weight: bold\">&lt;?php<\/span> wp_ozh_getCountryName<span style=\"color: #009900\">&#040;<\/span><span style=\"color: #cc66cc\">1<\/span><span style=\"color: #339933\">,<\/span><span style=\"color: #000088\">$comment<\/span><span style=\"color: #339933\">-&gt;;<\/span>comment_author_IP<span style=\"color: #009900\">&#041;<\/span> <span style=\"color: #000000;font-weight: bold\">?&gt;<\/span><\/div><\/li>\n<\/ol>\t<\/div>\n\n<\/div>\n\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 id=\"ig-sh-4\" class=\"syntax_hilite\">\n\n\t\t<div class=\"toolbar\">\n\n\t\t<div class=\"view-different-container\">\n\t\t\t\t\t\t<a href=\"#\" class=\"view-different\">&lt; View <span>plain text<\/span> &gt;<\/a>\n\t\t\t\t\t<\/div>\n\n\t\t<div class=\"language-name\">php<\/div>\n\n\t\t\n\t\t<br clear=\"both\">\n\n\t<\/div>\n\t\n\t<div class=\"code\">\n\t\t<ol class=\"php\" style=\"font-family:monospace\"><li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #000000;font-weight: bold\">&lt;?php<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #b1b100\">echo<\/span> <span style=\"color: #0000ff\">'&lt;img alt=&quot;your flag&quot; src=&quot;\/images\/flags\/flag_'<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; &nbsp;<span style=\"color: #339933\">.<\/span> wp_ozh_getCountryCode<span style=\"color: #009900\">&#040;<\/span><span style=\"color: #cc66cc\">0<\/span><span style=\"color: #339933\">,<\/span><span style=\"color: #000088\">$comment<\/span><span style=\"color: #339933\">-&gt;<\/span><span style=\"color: #004000\">comment_author_IP<\/span><span style=\"color: #009900\">&#041;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; &nbsp;<span style=\"color: #339933\">.<\/span> <span style=\"color: #0000ff\">'.gif&quot;&gt;'<\/span> <span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #000000;font-weight: bold\">?&gt;<\/span><\/div><\/li>\n<\/ol>\t<\/div>\n\n<\/div>\n\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}]}}