How to Change Website Content Based on Visitor Country

How to Change Website Content Based on Visitor Country

Do you want to show website content for specific countries?  You are in the right place, You can dynamically change website content for various locations. In this article, we will show  How to Change Website Content Based on Visitor Country. 

Sometimes, we targeted customers from specific countries. Like affiliate marketing, We need to target specific locations for our product.  We are able to show different content for different countries. Just imagine that You set content or image for the USA but when another country visitor visits those webpages you won’t benefit. So, You can set different content for different locations.

Gather Visitor location data:

We can gather visitor information by IP address (An Internet Protocol address is a numerical label such as that is connected to a computer network that uses the Internet Protocol for communication).   We can use API to gather IP information.

To get an IP address, Use this function on your functions.php:

function wpk_getUserIP() {
    if( array_key_exists('HTTP_X_FORWARDED_FOR', $_SERVER) && !empty($_SERVER['HTTP_X_FORWARDED_FOR']) ) {
        if (strpos($_SERVER['HTTP_X_FORWARDED_FOR'], ',')>0) {
            $addr = explode(",",$_SERVER['HTTP_X_FORWARDED_FOR']);
            return trim($addr[0]);
        } else {
            return $_SERVER['HTTP_X_FORWARDED_FOR'];
    else {
        return $_SERVER['REMOTE_ADDR'];

  This function will return a visitor’s IP address. Now we will locate visitors by their IP address. 

To get Visitor data, Use this function on your functions.php:

function wpk_user_loation_data($data="country"){
$res = file_get_contents(''.wpk_getUserIP());
$res = json_decode($res);
return $res->$data;

This function will return specific visitor data. Like Country, IP, Country code, City, Latitude, Longitude, Time zone, Postal code, etc.

To return visitor Country name:


Set Country Based Content:

Now we can get visitor data easily. Set up the content using the “if” condition. Just think about it, We want to sell a product for US customers. We can set up two different content, one set up for the USA another one for the global customer.

For example, we will show some country information for visitors from different countries. Like when US visitors visit the web page, it will show only their country information. 


<h1>Wellcome to <?php echo wpk_user_loation_data();?></h1>
$country = wpk_user_loation_data('country_code');
if($country == 'US'):
<p>The U.S. is a country of 50 states covering a vast swath of North America, with Alaska in the northwest and Hawaii extending the nation’s presence into the Pacific Ocean. Major Atlantic Coast cities are New York, a global finance and culture center, and capital Washington, DC. Midwestern metropolis Chicago is known for influential architecture and on the west coast, Los Angeles Hollywood is famed for filmmaking</p>
<?php endif; ?>
<?php if($country == 'BD'): ?>
<p>Bangladesh, to the east of India on the Bay of Bengal, is a South Asian country marked by lush greenery and many waterways. Its Padma (Ganges), Meghna and Jamuna rivers create fertile plains, and travel by boat is common. On the southern coast, the Sundarbans, an enormous mangrove forest shared with Eastern India, is home to the royal Bengal tiger.</p>
<?php endif; ?>
<?php if($country == 'IN'): ?>
<p>India, officially the Republic of India, is a country in South Asia. It is the seventh-largest country by area, the second-most populous country, and the most populous democracy in the world.</p>
<?php endif; ?>

You can use it for additionally purposes: 

  1. You can setup product page for specific country
  2. Set ads or affiliate link for specific country
  3. Add additional message or information for every located 
  4. Set posts for specific countries. etc


Most marketers use the same product for the global user. But different locations have different tastes. It’s more efficient than if we set a product by customer interest, We need to utilize different location customer’s interests. If you use these tricks, Your sales ratio will increase.  We hope you know “How to Change Website Content Based on Visitor Country”.

We can use these tricks for any kind of website. You get this idea for improving WordPress themes with increased product sales. If you like this article, and if you want more articles about this idea with more specific uses. please comment below.

See More:

How to increase WordPress memory limits

How to create popular post widgets without plugin in WordPress


  1. I’m really enjoying the theme/design of your web site. Do you ever run into any browser compatibility problems? A couple of my blog visitors have complained about my website not working correctly in Explorer but looks great in Opera. Do you have any solutions to help fix this problem?

  2. An interesting discussion is worth comment. I think that you should write more on this topic, it might not be a taboo subject but generally people are not enough to speak on such topics. To the next. Cheers

Leave a Reply

Your email address will not be published. Required fields are marked *