Shortcode for Geo Filtering

May 11, 2021
Darko G.

I started to develop IP Location Block as a security purpose plugin so as to protect the back-end of the site. And since version 3.0.0, it has been equipped with the functionality of front-end protection based on this suggestion at the support forum.

This helped to greatly improve the protection ability of this plugin against attacks via the front-end of the site.

Need for content filtering by country

Meanwhile, this also helped to make the purpose of this plugin ambiguous. Because the function of this plugin to specifying the validation target with the page, post type, category, and tab is not enough for a user who wants to manage contents by Geo-blocking.

Front-end validation target settings

For example, this discussion indicated that not only categories or tags but also custom taxonomies are needed to specify the targets.


But I don’t like to extend the functionality in this direction because I prefer to keep this plugin simple.

Then what's the solution?

Shortcode for Geo Filtering

Well, this plugin has much extendability enough to satisfy the demand. You can embed the following snippet for shortcode into your functions.php to manage the content:

if ( class_exists( 'IP_Location_Block' ) ) {
    function my_filter_content( $args, $content = null ) {

        // get settings and the geolocation of visitor
        $settings = IP_Location_Block::get_option();
        $geo = IP_Location_Block::get_geolocation();

        // return alternative content when the contry code IS NOT in the white list
        if ( FALSE === strpos( $settings['public']['white_list'], $geo['code'] ) ) {

            // set alternative content for not whilelisted countries
            extract( shortcode_atts( array(
                'alt' => "<p>Sorry, but you can't access this content.</p>",
            ), $args ) );

            return wp_kses_post( $alt );

        // return content when the contry code IS in the white list
        return $content;

    add_shortcode( 'ip-location-block', 'my_filter_content' );

The usage of the shortcode [ip-location-block] is as follows:

[ip-location-block alt="<img src='/image/alternative.png' alt='This content is not allowed in your country.'>"]

<p>This is a content for whitelisted countries.</p>


And there’s a tweak to keep the whitelist or blacklist of country code in “Front-end target settings”. You need 2 steps to do this:

  1. Check “Block by country” and specify the others. Then “Save Changes”.
  2. Uncheck “Block by country” and “Save Changes” once again.

Front-end target settings for geo filtering

Have fun 😄

Leave a Reply

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

© 2024 - IP Location Block by IDEOLOGIX Media