Shortcode for Geo Filtering

I started to develop IP Geo 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 support forum.

This helped to greatly improved the protection ability of this plugin against the attacks via 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 specifing 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.

WP-Property

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

Then what’t the solution?

Shortcode for Geo Filtering

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

<?php
if ( class_exists( 'IP_Geo_Block' ) ) {
    function my_filter_content( $args, $content = null ) {

        // get settings and the geolocation of visitor
        $settings = IP_Geo_Block::get_option();
        $geo = IP_Geo_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-geo-block', 'my_filter_content' );
}

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

[ip-geo-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>

[/ip-geo-block]

And there’s a tweak to keep the whitelist or blacklist of coutnry 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 emoji