Show Dynamic Content as per Page/Post and Visitor’s Country

Show different content on different pages to people of different countryIf you’ve a WordPress based website and you want to show dynamic content (different content for different people) based on your site’s page/content and/or user’s location (country), then this tutorial is for you. WordPress has proved to be effective in developing web and creating websites, same as Android has become famous as a usable mobile OS. You must be familiar with WordPress’ powerful features and ease if you’re building your website using it and possibly, that is the only reason that you opted for WordPress. Why you need dynamic content? To show different links/greeting to people of different countries or to show different ads to different people. You can also use dynamic content to show different ads on different pages, like ads related to Software on software pages and ads related to Web Hosting on web hosting/blogging pages. This way, you can show more ads on less pages and more over, your ads will be targeted towards your visitors. Read on to know how to easily implement dynamic content with WordPress.

Profit in showing Dynamic Content

When showing content pages wise

  • Show different content on home page and other pages.
  • Show different content according to tags/categories.
  • Show different ads according to tags/categories.

When showing content country wise

  • Show different content to people of different countries.
  • Show localized/targeted ads to people of different countries.

What you should know before following this tutorial?

Absolutely nothing! You just need know to how to handle WordPress (even little knowledge of it will be enough) and how to login to your website’s dashboard. No other knowledge is required on your part to follow this tutorial. I’ll guide you at every step with screenshots so that you can implement dynamic content on your website, even if you don’t have any knowledge about HTML or PHP.

The Method of Dynamic Content!

We’ll be using plugins along with coding to make things easier for everyone. As I foretold, this tutorial will teach you to easily implement dynamic content on your website with WordPress. We won’t be messing up with WordPress core files or your websites’ code. We’ll be keeping things simpler and use an innovative approach to get solution for our problem. NOTE: In this tutorial, we’re implementing dynamic content via PHP, so, you can get some undesired results if you’ve some page caching plugins installed. You need to disable/remove them for getting this tutorial work properly for your site.

We’ll be using these two plugins for our help:
GT-Geo Targeting : http://wordpress.org/extend/plugins/gt-geo-targeting/
WP PHP Widget : http://wordpress.org/extend/plugins/wp-php-widget/

Installing these plugins

Please follow these steps for each of the plugin. You can install them in any of the following ways:

  • Goto your website’s Dashboard -> Plugins -> Add new -> Search. Search for the given plugins and click ‘Install’ buttons in the search results to install them.
  • Download these plugins from link specified above. Goto Dashboard -> Plugins -> Add new -> Upload. Upload the downloaded file and plugin will get installed.

Implementing dynamic content

Show different content for people of different countries in your posts/pages

There are two ways how you can show different content to people of different countries. Using GT-Geo plugin, you can show some content to specific country’s people or you can hide some content from specific country’s people. When you want to show content to people of some countries, then you should use [geo-in] shortcode, which make you show the content only to people of specific countries. When you want to show content to people of many countries and hide from people of some countries, then you should use [geo-out] shortcode, which make you hide content from people of that specified countries. As you’ll realize after some time, [geo-in] and [geo-out] works quite opposite to each other.

[geo-in] method

[geo-in country=”<Countries name or shortcode listed by use of comma>” note=”<Note to show to people of unspecified countries>”] <Content to show to people of specified countries> [/geo-in]

For example:

[geo-in country=”us”] Hello USA [/geo-in]

This will show “Hello USA” to people of US and “Content not available for your country” to others (as note is not provided).

[geo-in country=”us, canada”] Hello North America visitors [/geo-in]

This will show “Hello North America visitors” to people of US and Canada; and “Content not available for your country” to others (as note is not provided).

[geo-in country=”us, canada” note=”Hello visitor”] Hello North America visitors [/geo-in]

This will show “Hello North America visitors” to people of US and Canada; and “Hello visitor” to people of other countries (as note is provided as “Hello visitor”).

[geo-out] method

[geo-out country=”<Countries name or shortcode listed by use of comma>” note=”<Note to show to people of specified countries>”] <Content to show to people of not specified countries> [/geo-out]

For example:

[geo-in country=”us”] Hello USA [/geo-out]

This will show “Hello USA” to people outside of US and “Content not available for your country” for US citizens (as note is not provided).

[geo-out country=”us,canada”] Hello visitors [/geo-out]

This will show “Hello visitors” to people outside of US and Canada; and “Content not available for your country” to US and Canada citizens (as note is not provided).

[geo-out country=”us,canada” note=”Hello North America visitor”] Hello visitors [/geo-out]

This will show “Hello visitors” to people outside of US and Canada; and “Hello North America visitor” to people of US and Canada (as note is provided as “Hello North America visitor”).

Show different content for people of different countries in your sidebar

  1. Goto your website’s dashboard
  2. Goto Appearance -> Widgets
  3. Hold and move Text widget or PHP widget to the sidebar
  4. Write geo-in/geo-out code in that widget. Check the working of geo-in and geo-out by opening your website and see which content is being shown to you.

Show different content on different pages in your sidebar

We’ll be using WordPress functions to get our job done in this section. The basic logic we’re going to use here is that we’ll check that which page is being viewed and accordingly, we’ll display the required content for each type of pages.

WordPress conditional functions

You must be thinking why we need to learn WordPress functions, when I foretold that tutorial will be easy. We’re going to use these functions to check where is visitor on your website and show the content accordingly. Just watch them working (examples) and you’ll easily learn how they work. NOTE: These all are PHP functions and must be written inside PHP block (), else they won’t work and also,  error will be shown.

is_home()

Checks if it is the home page of the website.

is_page()

Checks if the post is a page.

is_page(“Hello Ubuntu”)

Checks if the page name is “Hello Ubuntu”

$page_array=array(“Hello Ubuntu”,”Hello Linux”,”Hello Ashutosh”);
is_page($page_array)

Checks if the page has any of the name from this $page_array or not. This $page_array should be defined before this line, else it cause an error.

is_single()

Checks if it is a post, i.e., it is a single post.

is_single(“Hello Ubuntu”)

Checks if the post name is “Hello Ubuntu”.

$post_array=array(“Hello Ubuntu”,”Hello Linux”,”Hello Ashutosh”);
is_single($post_array)

Checks if the post has any of the name from this $post_array or not. This $post_array should be defined before this line, else it cause an error.

has_tag()

Checks if the post has any tags or not.

has_tag(“Software”)

Checks if the post has specified tag “Software” or not.

$tag_array=array(“Software”,”Tools”,”Utilities”);
has_tag($tag_array)

Checks if the post has any of the tag from this $tag_array or not. This $tag_array should be defined before this line, else it cause an error.

has_category()

Checks if the post has any categories or not.

has_category(“Software”)

Checks if the post has specified category “Software” or not.

$category_array=array(“Software”,”Tools”,”Utilities”);
has_category($category_array)

Checks if the post has any of the category from this $category_array or not. This $category_array should be defined before this line, else it cause an error.

PHP codes

<?php

if ( <any-WordPress-conditional-function-as-your-requirement> == true ) {

echo ‘ <what-to-show-if-condition-is-true> ‘;

} ?>

For example:

<?php

if (is_home() == true) {

echo ‘ You’re at homepage. ‘;

} ?>

This will show a message ‘You’re at homepage’ only if a visitor is at homepage, else won’t show anything.

<?php

if (has_tag(“Software”) == true) {

echo ‘ You’re at reading articles about Software. ‘;

} ?>

This will show a message ‘You’re at reading articles about Software’ only if a visitor is at pages which has got Software tag, else won’t show anything.

<?php

$category_array=array(“Software”,”Tools”,”Utilities”);

if (has_category($category_array) == true) {

echo ‘ You’re reading about Software, Tools or Utilities. ‘;

} ?>

This will show a message ‘You’re reading about Software, Tools or Utilities’ only on pages which has Software/Tools/Utilities category, else won’t show anything.

<?php

$category_array=array(“Software”,”Tools”,”Utilities”);

if (has_category($category_array) == true) {

echo ‘ <advertisement code in HTML or JavaScript> ‘;

} ?>

This will show ads only on pages which has Software/Tools/Utilities category, else won’t show anything.

Showing dynamic content in sidebar

  • Goto your website’s dashboard
  • Goto Appearance -> Widgets
  • Hold and more Text widget or PHP widget to the sidebar
  • Write PHP code along with functions in the PHP widget to show contents according to the page/tag/category.

Show content on different pages/posts and for different countries’ people

We’ll be joining both the above tricks to build a better one – to show dynamic content on different pages/posts for people of different countries. If you’ve gone through above sections, then you’ll find it easy, using this joint trick. We’ll be using geo-in and geo-out shortcodes in the PHP echo command to make a code that will only show content to people of specific country only when they browse selected pages.

[geo-in] + conditional-function method

<?php

if ( <any-WordPress-conditional-function-as-your-requirement> == true ) {

echo ‘

[geo-in country=”<country list separated by comma>” note=”<message to show to people from unspecified countries>”] <content to show to people of specified countries> [/geo-in]

‘;

} ?>

[geo-out] + conditional-function method

<?php

if ( <any-WordPress-conditional-function-as-your-requirement> == true ) {

echo ‘

[geo-out country=”<country list separated by comma>” note=”<message to show to people from specified countries>”] <content to show to people of unspecified countries> [/geo-out]

‘;

} ?>

[geo-in] and [geo-out] + conditional-function method

<?php

if ( <any-WordPress-conditional-function-as-your-requirement> == true ) {

echo ‘

[geo-in country=”<country list separated by comma>” note=”<message to show to people from unspecified countries>”] <content 1 to show to people of specified countries> [/geo-in]

[geo-out country=”<country list separated by comma>” note=”<message to show to people from specified countries>”] <content 1 to show to people of unspecified countries> [/geo-out]

‘;

} ?>

multiple [geo-in] and [geo-out] + conditional-function method

<?php

if ( <any-WordPress-conditional-function-as-your-requirement> == true ) {

echo ‘

[geo-in country=”<country list separated by comma>” note=”<message to show to people from unspecified countries>”] <content 1 to show to people of specified countries> [/geo-in]

[geo-out country=”<country list separated by comma>” note=”<message to show to people from specified countries>”] <content 2 to show to people of unspecified countries> [/geo-out]

[geo-out country=”<country list separated by comma>” note=”<message to show to people from specified countries>”] <content 3 to show to people of unspecified countries> [/geo-out]

‘;

} ?>

multiple [geo-in] and [geo-out] + multiple conditional-function method

<?php

if ( <any-WordPress-conditional-function-ONE-as-your-requirement> == true ) {

echo ‘

[geo-in country=”<country list separated by comma>” note=”<message to show to people from unspecified countries>”] <content 1 to show to people of specified countries> [/geo-in]

[geo-out country=”<country list separated by comma>” note=”<message to show to people from specified countries>”] <content 2 to show to people of unspecified countries> [/geo-out]

[geo-out country=”<country list separated by comma>” note=”<message to show to people from specified countries>”] <content 3 to show to people of unspecified countries> [/geo-out]

‘;

}

if ( <any-WordPress-conditional-function-TWO-as-your-requirement> == true ) {

echo ‘

[geo-in country=”<country list separated by comma>” note=”<message to show to people from unspecified countries>”] <content 1 to show to people of specified countries> [/geo-in]

[geo-out country=”<country list separated by comma>” note=”<message to show to people from specified countries>”] <content 2 to show to people of unspecified countries> [/geo-out]

[geo-in country=”<country list separated by comma>” note=”<message to show to people from unspecified countries>”] <content 3 to show to people of specified countries> [/geo-in]

‘;

} ?>

Implementing dynamic content on different pages according to different countries

You can use above given formats/codes to display various dynamic content on different pages/posts of your website to people of different countries without any problem. I’ll be illustrating some examples to give you an idea how to make it work for you.

<? php

if (is_home() == true) { /* checks if it is home page */
echo ‘

[geo-in country=”us” note=”You’re not from US.”] Hi US citizen. [/geo-in]

‘;
}

if (is_single() == true) { /* checks if it is a single post */
echo ‘

[geo-in country”us” note”You’re from outside US.”] Hello America people. [/geo-out]

‘;
}

?>

This way, you’re showing content according to country but only on certain pages and not whole website. As you can see, above example will work in this way:

  1. If home page, then: (a) if visitor from US then say “Hi US citizen” (b) if visitor from other countries then say “You’re not from US.”
  2. If a single post, then: (a) if visitor from US then say “Hello America people.” (b) if visitor from other countries then say “You’re from outside US.”
  3. If other page on whole website: no content is shown.

<?php

if (is_home() == true) { /* checks if it is home page */
echo ‘

[geo-in country=”us” note=””] All type ads for US citizen. [/geo-in]

‘;
}

if (has_tag(“Software”) == true) { /* checks if post has Software tag */
echo ‘

[geo-in country”us” note””] Software ads for US citizen. [/geo-out]

‘;
}

?>

This way, you’ll be showing ads on different pages for different people. NOTE: Blank notes (note=””) in above examples is used so that no message is shown to non-US citizens. This confirms that unspecified people (here, non-US) won’t be able to know that you’re showing ads to US citizens.

Above example will work like:

  1. If home page, then: (a) if visitor from US then only show all types ads for US citizens.
  2. If post has Software tag, then: (a) if visitor from US then only show Software types ads for US citizens.
  3. If any other post/page on whole website: no ads/content is shown.

Any questions

Hope you enjoyed this tutorial. I know this is little cumbersome but it can’t have been easier than this. If you follow this tutorial readily and give care while copying/using given codes, you will be easily making your website, bundled with dynamic content/advertisements. Personally, I like this trick (may be because I created it) because by using this, you can place ads of multiple companies/sponsors on your website without affecting your user’s site experience. This trick can also be used in templates but that is not a part of this tutorial.

Got a problem? Fell free to post your queries/suggestions via comments. I always look forward for your feedbacks. If you use this tutorial for any other purpose, then please let me know via comments. I would like to hear its growing potential.

Similar Posts