Skip to main content

Post categories

  • Drupal Tips & Tricks
  • Drupal Howtos
  • Drupal Themes
  • X Twitter
  • youtube
Home
Drupal 4U
Drupal Development, Drupal Web Design, Drupal SEO
  • Home
  • Showcases
  • Services
  • Blog
  • Themes
  • About
  • Contact

Breadcrumb

  1. Home
  2. HowTos
  3. How to display Drupal blocks based on location - Drupal geolocation block

How to display Drupal blocks based on location - Drupal geolocation block

HowTos
June 05, 2016
Profile picture for user drupal
By drupal
  • facebook-f
  • twitter
  • envelope
  • print
194

This howto shows you a easy solution to display / not display Drupal blocks based on the location of the user (location based display of Drupal content) with the help of the Drupal Smart IP module. Smart IP identify visitor's geographical location (longitude/latitude), country, region, city and postal code based on the IP address of the user.

Solution

Step 1: Install and configure the module Smart IP

The module Smart IP allows you to identify the users location with a very broad range of data sources.

https://www.drupal.org/project/smart_ip

Install the module, choose a method and configure the module accordingly.

Create a block and apply PHP code

Create a block, change the text format to "PHP code" and add the following PHP code:

<?php
$smart_ip_session = smart_ip_session_get('smart_ip');
if ($smart_ip_session['location']['country_code'] == 'XX') {
echo '';
print "This Image block only displays in the country XX";
}
?>

"XX" represents the country code to be changed. You can add multiple country codes, allowing you to define in which countries the Drupal block shall be shown. You can add any PHP commands after the if logic to display or execute whatever you want. You can also change the logic to "if not" allowing you to not display the Drupal block in certain countries.

How to display Drupal blocks based on location - Drupal geolocation block
Modules

Read more articles

Newer
How to make a click-to-call link for mobile Devices only using HTML/CSS
Older
How to create a Drupal view with future events / dates of nodes
  • Log in to post comments
Profile picture for user drupal
drupal
194
1
min read
A- A+
  • facebook-f
  • twitter
  • envelope
  • print

Contact Drupal 4U

Get in touch for questions and our Drupal development, design and SEO services.

Contact Us

We aim to reply the latest within 48h.

About Drupal 4U

We are a group of Drupal designers, developers and webmasters and we launched this website to share our experience and knowledge with you. Drupal is a free software package that allows you to easily organize, manage and publish your content, with an endless variety of customization. Drupal is open source software maintained and developed by a very large community of developers and users.

  • X Twitter
  • youtube

Drupal Services

  • Drupal Web Design
  • Drupal Development
  • Drupal SEO
  • Drupal Migration

Drupal 4U

  • Services
  • Showcases
  • Themes
  • Blog
  • About

Drupal Links

  • Official Drupal Website
  • Drupal Answers
  • More Than Themes
  • Theme Forest
  • DXPR: Drupal for Marketers
2012 - 2025, Drupal 4U

Footer menu

  • Contact
Home
Drupal 4U
Drupal Development, Drupal Web Design, Drupal SEO
  • Home
  • Showcases
  • Services
  • Blog
  • Themes
  • About
  • Contact
Clear keys input element