Skip to main content
  • X Twitter
  • youtube
Home
Drupal 4U
Built for AI. Powered by Drupal.
  • 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
203

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
203
1
min read
A- A+
  • facebook-f
  • twitter
  • envelope
  • print

Contact Us

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

Contact Us

We aim to reply the latest within 48h.

Drupal 4U: Built for AI. Powered by Drupal.

We are a group of Drupal designers, developers and webmasters. We are Drupal experts! We provide comprehensive services that go beyond traditional web development, focusing on how AI understands, interprets, and cites your content. With our deep expertise in Drupal's architecture, we build the intelligent core of your digital presence, ensuring your brand remains authoritative and visible in a world where AI is the primary gatekeeper of information. Partner with us and let's future-proof your digital strategy together.

  • X Twitter
  • youtube

Drupal Services

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

Drupal 4U

  • Services
  • Showcases
  • Themes
  • Blog
  • About

Drupal Links

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

Footer menu

  • Contact
Home
Drupal 4U
Built for AI. Powered by Drupal.
  • Home
  • Showcases
  • Services
  • Blog
  • Themes
  • About
  • Contact
Clear keys input element