title: Insert PHP code In WordPress Page and Post
body: <p>I want to know the <em>visitor</em> country using PHP and display it in on a WordPress Page. But when I add PHP code to a WordPress page or post it gives me an error.</p>
  <p>How can we add PHP code on WordPress pages and posts?</p>
  <pre><code>&lt;?PHP
      try
      {
          function visitor_country()
          {
              $client  = @$_SERVER['HTTP_CLIENT_IP'];
              $forward = @$_SERVER['HTTP_X_FORWARDED_FOR'];
              $remote  = $_SERVER['REMOTE_ADDR'];
              $result  = &quot;Unknown&quot;;
              if(filter_var($client, FILTER_VALIDATE_IP))
              {
                  $ip = $client;
              }
              elseif(filter_var($forward, FILTER_VALIDATE_IP))
              {
                  $ip = $forward;
              }
              else
              {
                  $ip = $remote;
              }
  
              $ip_data = @json_decode(file_get_contents(&quot;http://www.geoplugin.net/json.gp?ip=&quot; . $ip));
  
              if($ip_data &amp;&amp; $ip_data-&gt;geoplugin_countryName != null)
              {
                  $result = array('ip' =&gt; $ip,
                                  'continentCode' =&gt; $ip_data-&gt;geoplugin_continentCode,
                                  'countryCode' =&gt; $ip_data-&gt;geoplugin_countryCode,
                                  'countryName' =&gt; $ip_data-&gt;geoplugin_countryName,
                                  );
              }
              return $result;
          }
  
  
          $visitor_details = visitor_country(); // Output Country name [Ex: United States]
          $country = $visitor_details['countryName'];
  </code></pre>