{"id":5119,"date":"2017-12-31T10:12:19","date_gmt":"2017-12-31T15:12:19","guid":{"rendered":"https:\/\/themespress.ca\/?p=5119"},"modified":"2022-10-01T14:44:39","modified_gmt":"2022-10-01T18:44:39","slug":"how-to-hide-a-form-based-on-a-countrys-ip","status":"publish","type":"post","link":"https:\/\/themespress.ca\/en\/how-to-hide-a-form-based-on-a-countrys-ip\/","title":{"rendered":"How To Hide a Form Based on a Country&#8217;s IP"},"content":{"rendered":"<div class=\"wpb-content-wrapper\"><div class=\"vc_row wpb_row vc_row-fluid\"><div class=\"wpb_column vc_column_container vc_col-sm-12\"><div class=\"vc_column-inner\"><div class=\"wpb_wrapper\">\n\t<div class=\"wpb_text_column wpb_content_element\" >\n\t\t<div class=\"wpb_wrapper\">\n\t\t\t<p>With this tutorial, we will show you how to hide things, such as forms, DIV tags or messages, based on the user&#8217;s geolocalisation. In this example, we&#8217;ll hide things if the user is in <strong>Canada<\/strong>.<\/p>\n<p>If the example below we will create a Formidable Form shortcode which will display a form if the user&#8217;s IP is in Canada. Otherwise, we will display a form that says that they cannot fill out the form.<\/p>\n<p>The first condition that we need is to fetch the user&#8217;s IP given that it is mandatory to make our condition work.<\/p>\n\n\t\t<\/div>\n\t<\/div>\n<h2 style=\"text-align: left\" class=\"vc_custom_heading vc_do_custom_heading\" >Afficher l\u2019IP du client<\/h2><div id=\"ts-enlighterjs-container-3143734\" class=\"ts-enlighterjs-container-single-enabled  \" style=\"width: 100%;  margin-top: 0px; margin-bottom: 0px;\" data-enlighter-doubleclick=\"true\" data-enlighter-windowbutton=\"true\" data-enlighter-windowtext=\"New Window\" data-enlighter-rawbutton=\"true\" data-enlighter-rawtext=\"RAW Code\" data-enlighter-infobutton=\"false\" data-enlighter-infotext=\"EnlighterJS\" data-enlighter-indent=\"2\"><pre id=\"ts-enlighterjs-pre-3143734\" class=\"\" style=\"white-space: pre-wrap; height: 100%; margin: 0; padding: 0;\" data-enlighter-language=\"php\" data-enlighter-theme=\"enlighter\" data-enlighter-group=\"\" data-enlighter-title=\"\" data-enlighter-linenumbers=\"true\" data-enlighter-lineoffset=\"1\" data-enlighter-highlight=\"\">&lt;?php $ip = $_SERVER[&#039;REMOTE_ADDR&#039;]; echo $ip; ?&gt;<\/pre><\/div>\n\t<div class=\"wpb_text_column wpb_content_element\" >\n\t\t<div class=\"wpb_wrapper\">\n\t\t\t<p>Now we will create a function that will be called by the Json file, a geolocalisation API to get the IP, the country where the user is based. In our condition we will then specify if the user country is Canada, display the form&#8217;s shortcode otherwise display a message.<\/p>\n\n\t\t<\/div>\n\t<\/div>\n<h2 style=\"text-align: left\" class=\"vc_custom_heading vc_do_custom_heading\" >Cacher un formulaire si Nom de Pays \u2013 if Show \/ Hide form ip country<\/h2><div id=\"ts-enlighterjs-container-3405800\" class=\"ts-enlighterjs-container-single-enabled  \" style=\"width: 100%;  margin-top: 0px; margin-bottom: 0px;\" data-enlighter-doubleclick=\"true\" data-enlighter-windowbutton=\"true\" data-enlighter-windowtext=\"New Window\" data-enlighter-rawbutton=\"true\" data-enlighter-rawtext=\"RAW Code\" data-enlighter-infobutton=\"false\" data-enlighter-infotext=\"EnlighterJS\" data-enlighter-indent=\"2\"><pre id=\"ts-enlighterjs-pre-3405800\" class=\"\" style=\"white-space: pre-wrap; height: 100%; margin: 0; padding: 0;\" data-enlighter-language=\"php\" data-enlighter-theme=\"enlighter\" data-enlighter-group=\"\" data-enlighter-title=\"\" data-enlighter-linenumbers=\"true\" data-enlighter-lineoffset=\"1\" data-enlighter-highlight=\"\">&lt;?php \/\/ Fonction de donn&eacute;es g&eacute;olocalis&eacute;es \nfunction resolveIP($ip) { \n\t\n\t$string = file_get_contents(&quot;https:\/\/ipsidekick.com\/{$ip}&quot;); \n\t$json = json_decode($string); return $json; } \/\/ On retourne la valeur de l&#039;IP en appelant notre fonction \n\t$ip = $_SERVER[&#039;REMOTE_ADDR&#039;]; \n\t$json = resolveIP($ip); \/\/ Si le nom du pays de l&#039;IP client est Canada affiche le formulaire \n\t\n\tif ($json-&gt;country-&gt;name == &#039;Canada&#039;) {\n\t   echo do_shortcode(&#039;[formidable id=3]&#039;);\n\t}\n\t \n\t\/\/ Sinon\n\telse {\n\t\techo &#039;Seules les entreprises au Canada sont accept&eacute;es.&#039;;\n\t} \n}\n?&gt;<\/pre><\/div>\n\t<div class=\"wpb_text_column wpb_content_element\" >\n\t\t<div class=\"wpb_wrapper\">\n\t\t\t<p>We will do the same function but this time we will replace the country code from row 14:<\/p>\n\n\t\t<\/div>\n\t<\/div>\n<h2 style=\"text-align: left;font-family:Abril Fatface;font-weight:400;font-style:normal\" class=\"vc_custom_heading vc_do_custom_heading\" >How To Hide a Form Based on a Country's IP<\/h2><div id=\"ts-enlighterjs-container-3985011\" class=\"ts-enlighterjs-container-single-enabled  \" style=\"width: 100%;  margin-top: 0px; margin-bottom: 0px;\" data-enlighter-doubleclick=\"true\" data-enlighter-windowbutton=\"true\" data-enlighter-windowtext=\"New Window\" data-enlighter-rawbutton=\"true\" data-enlighter-rawtext=\"RAW Code\" data-enlighter-infobutton=\"false\" data-enlighter-infotext=\"EnlighterJS\" data-enlighter-indent=\"2\"><pre id=\"ts-enlighterjs-pre-3985011\" class=\"\" style=\"white-space: pre-wrap; height: 100%; margin: 0; padding: 0;\" data-enlighter-language=\"php\" data-enlighter-theme=\"enlighter\" data-enlighter-group=\"\" data-enlighter-title=\"\" data-enlighter-linenumbers=\"true\" data-enlighter-lineoffset=\"1\" data-enlighter-highlight=\"\">&lt;?php \n\/\/ Fonction de donn&eacute;es g&eacute;olocalis&eacute;es \nfunction resolveIP($ip) { \n\t$string = file_get_contents(&quot;https:\/\/ipsidekick.com\/{$ip}&quot;); \n\t$json = json_decode($string); return $json; } \/\/ On retourne la valeur de l&#039;IP en appelant notre fonction \n\t$ip = $_SERVER[&#039;REMOTE_ADDR&#039;]; \n\t$json = resolveIP($ip); \/\/ Si le code du pays de l&#039;IP client est Canada (CA) affiche le formulaire \n\t\n\tif ($json-&gt;country-&gt;code == &#039;CA&#039;) {\n\t   echo do_shortcode(&#039;[formidable id=3]&#039;);\n\t}\n\t\/\/ Sinon\n\t\n\telse {\n\t    echo &#039;Seules les entreprises au Canada sont accept&eacute;es.&#039;;\n\t} \n}\n?&gt;<\/pre><\/div>\n\t<div class=\"wpb_text_column wpb_content_element\" >\n\t\t<div class=\"wpb_wrapper\">\n\t\t\t<p>You decide which formula better suits your needs.<\/p>\n\n\t\t<\/div>\n\t<\/div>\n<\/div><\/div><\/div><\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"With this tutorial, we will show you how to hide things, such as forms, DIV tags or messages, based on the user's geolocalisation. In this example, we'll hide things if<div class=\"read-more\"><a class=\"btn button-secondary\" href=\"https:\/\/themespress.ca\/en\/how-to-hide-a-form-based-on-a-countrys-ip\/\">Read More<\/a><\/div>","protected":false},"author":81,"featured_media":5122,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[283],"tags":[320],"class_list":["post-5119","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials","tag-php-coding"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/posts\/5119","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/users\/81"}],"replies":[{"embeddable":true,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/comments?post=5119"}],"version-history":[{"count":0,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/posts\/5119\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/media\/5122"}],"wp:attachment":[{"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/media?parent=5119"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/categories?post=5119"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/tags?post=5119"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}