{"id":34,"date":"2021-05-11T00:28:29","date_gmt":"2021-05-11T00:28:29","guid":{"rendered":"http:\/\/iplocationblock.test\/2021\/05\/11\/shortcode-for-geo-filtering\/"},"modified":"2021-05-11T11:55:33","modified_gmt":"2021-05-11T11:55:33","slug":"shortcode-for-geo-filtering","status":"publish","type":"post","link":"https:\/\/iplocationblock.com\/shortcode-for-geo-filtering\/","title":{"rendered":"Shortcode for Geo Filtering"},"content":{"rendered":"<p>I started to develop <a title=\"WordPress \u203a IP Location Block \u00ab WordPress Plugins\" href=\"https:\/\/wordpress.org\/plugins\/ip-location-block\/\">IP Location Block<\/a> 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 <a title=\"Topic: Feature suggestion: Redirection for certain pages\/posts etc. \u00ab WordPress.org Forums\" href=\"https:\/\/wordpress.org\/support\/topic\/feature-suggestion-redirection-for-certain-pagesposts-etc\/\">this suggestion at the support forum<\/a>.<\/p>\n<p>This helped to greatly improve <a title=\"Analysis of Attack Vectors | IP Location Block\" href=\"https:\/\/iplocationblock.com\/analysis-of-attack-vector-against-wp-plugins\/\">the protection ability<\/a> of this plugin against attacks via the front-end of the site.<\/p>\n<p><!--more--><\/p>\n<h3 id=\"need-for-content-filtering-by-country\">Need for content filtering by country<\/h3>\n<p>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 <a title=\"Geo-blocking - Wikipedia\" href=\"https:\/\/en.wikipedia.org\/wiki\/Geo-blocking\">Geo-blocking<\/a>.<\/p>\n<p><img decoding=\"async\" title=\"Front-end validation target settings\" src=\"https:\/\/iplocationblock.com\/wp-content\/uploads\/2021\/05\/ValidationTarget.png\" alt=\"Front-end validation target settings\" \/><\/p>\n<p>For example, <a title=\"Topic: I need help because of the settings \u00ab WordPress.org Forums\" href=\"https:\/\/wordpress.org\/support\/topic\/i-need-help-because-of-the-settings\/\">this discussion<\/a> indicated that not only categories or tags but also <a title=\"Taxonomies \u00ab WordPress Codex\" href=\"https:\/\/codex.wordpress.org\/Taxonomies\">custom taxonomies<\/a> are needed to specify the targets.<\/p>\n<p><a title=\"WP-Property - WordPress Powered Real Estate and Property Management \u2014 WordPress Plugins\" href=\"https:\/\/wordpress.org\/plugins\/wp-property\/\"><img decoding=\"async\" title=\"WP-Property\" src=\"https:\/\/iplocationblock.com\/wp-content\/uploads\/2021\/05\/WP-Property.jpg\" alt=\"WP-Property\" \/><\/a><\/p>\n<p>But I don\u2019t like to extend the functionality in this direction because I prefer to keep this plugin simple.<\/p>\n<p>Then what&#8217;s the solution?<\/p>\n<h3 id=\"shortcode-for-geo-filtering\">Shortcode for Geo Filtering<\/h3>\n<p>Well, this plugin has much extendability enough to satisfy the demand. You can embed the following snippet for <a title=\"Shortcode \u00ab WordPress Codex\" href=\"https:\/\/codex.wordpress.org\/Shortcode\">shortcode<\/a> into your <code class=\"highlighter-rouge\">functions.php<\/code> to manage the content:<\/p>\n<figure class=\"highlight\">\n<pre><code class=\"language-php\" data-lang=\"php\"><span class=\"cp\">&lt;?php<\/span>\n<span class=\"k\">if<\/span> <span class=\"p\">(<\/span> <span class=\"nb\">class_exists<\/span><span class=\"p\">(<\/span> <span class=\"s1\">'IP_Location_Block'<\/span> <span class=\"p\">)<\/span> <span class=\"p\">)<\/span> <span class=\"p\">{<\/span>\n    <span class=\"k\">function<\/span> <span class=\"nf\">my_filter_content<\/span><span class=\"p\">(<\/span> <span class=\"nv\">$args<\/span><span class=\"p\">,<\/span> <span class=\"nv\">$content<\/span> <span class=\"o\">=<\/span> <span class=\"kc\">null<\/span> <span class=\"p\">)<\/span> <span class=\"p\">{<\/span>\n\n        <span class=\"c1\">\/\/ get settings and the geolocation of visitor\n<\/span>        <span class=\"nv\">$settings<\/span> <span class=\"o\">=<\/span> <span class=\"s1\">IP_Location_Block<\/span><span class=\"o\">::<\/span><span class=\"na\">get_option<\/span><span class=\"p\">();<\/span>\n        <span class=\"nv\">$geo<\/span> <span class=\"o\">=<\/span> <span class=\"s1\">IP_Location_Block<\/span><span class=\"o\">::<\/span><span class=\"na\">get_geolocation<\/span><span class=\"p\">();<\/span>\n\n        <span class=\"c1\">\/\/ return alternative content when the contry code IS NOT in the white list\n<\/span>        <span class=\"k\">if<\/span> <span class=\"p\">(<\/span> <span class=\"k\">FALSE<\/span> <span class=\"o\">===<\/span> <span class=\"nb\">strpos<\/span><span class=\"p\">(<\/span> <span class=\"nv\">$settings<\/span><span class=\"p\">[<\/span><span class=\"s1\">'public'<\/span><span class=\"p\">][<\/span><span class=\"s1\">'white_list'<\/span><span class=\"p\">],<\/span> <span class=\"nv\">$geo<\/span><span class=\"p\">[<\/span><span class=\"s1\">'code'<\/span><span class=\"p\">]<\/span> <span class=\"p\">)<\/span> <span class=\"p\">)<\/span> <span class=\"p\">{<\/span>\n\n            <span class=\"c1\">\/\/ set alternative content for not whilelisted countries\n<\/span>            <span class=\"nb\">extract<\/span><span class=\"p\">(<\/span> <span class=\"nx\">shortcode_atts<\/span><span class=\"p\">(<\/span> <span class=\"k\">array<\/span><span class=\"p\">(<\/span>\n                <span class=\"s1\">'alt'<\/span> <span class=\"o\">=&gt;<\/span> <span class=\"s2\">\"&lt;p&gt;Sorry, but you can't access this content.&lt;\/p&gt;\"<\/span><span class=\"p\">,<\/span>\n            <span class=\"p\">),<\/span> <span class=\"nv\">$args<\/span> <span class=\"p\">)<\/span> <span class=\"p\">);<\/span>\n\n            <span class=\"k\">return<\/span> <span class=\"nx\">wp_kses_post<\/span><span class=\"p\">(<\/span> <span class=\"nv\">$alt<\/span> <span class=\"p\">);<\/span>\n        <span class=\"p\">}<\/span>\n\n        <span class=\"c1\">\/\/ return content when the contry code IS in the white list\n<\/span>        <span class=\"k\">return<\/span> <span class=\"nv\">$content<\/span><span class=\"p\">;<\/span>\n    <span class=\"p\">}<\/span>\n\n    <span class=\"nx\">add_shortcode<\/span><span class=\"p\">(<\/span> <span class=\"s1\">'ip-location-block'<\/span><span class=\"p\">,<\/span> <span class=\"s1\">'my_filter_content'<\/span> <span class=\"p\">);<\/span>\n<span class=\"p\">}<\/span><\/code><\/pre>\n<\/figure>\n<p>The usage of the shortcode <code class=\"highlighter-rouge\">[ip-location-block]<\/code> is as follows:<\/p>\n<figure class=\"highlight\">\n<pre><code class=\"language-yaml\" data-lang=\"yaml\"><span class=\"pi\">[<\/span><span class=\"nv\">ip-location-block alt=\"&lt;img src='\/image\/alternative.png' alt='This content is not allowed in your country.'&gt;\"<\/span><span class=\"pi\">]<\/span>\n\n<span class=\"s\">&lt;p&gt;This is a content for whitelisted countries.&lt;\/p&gt;<\/span>\n\n<span class=\"pi\">[<\/span><span class=\"nv\">\/ip-location-block<\/span><span class=\"pi\">]<\/span><\/code><\/pre>\n<\/figure>\n<p>And there\u2019s a tweak to keep the whitelist or blacklist of country code in \u201c<strong>Front-end target settings<\/strong>\u201d. You need 2 steps to do this:<\/p>\n<ol>\n<li>Check \u201c<strong>Block by country<\/strong>\u201d and specify the others. Then \u201c<strong>Save Changes<\/strong>\u201d.<\/li>\n<li>Uncheck \u201c<strong>Block by country<\/strong>\u201d and \u201c<strong>Save Changes<\/strong>\u201d once again.<\/li>\n<\/ol>\n<p><img decoding=\"async\" title=\"Front-end target settings for geo filtering\" src=\"https:\/\/iplocationblock.com\/wp-content\/uploads\/2021\/05\/FrontEndSettings.png\" alt=\"Front-end target settings for geo filtering\" \/><\/p>\n<p>Have fun \ud83d\ude04<span class=\"emoji\"><br \/><\/span><\/p>\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-34","post","type-post","status-publish","format-standard","hentry","category-general"],"_links":{"self":[{"href":"https:\/\/iplocationblock.com\/wp-json\/wp\/v2\/posts\/34","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/iplocationblock.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/iplocationblock.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/iplocationblock.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/iplocationblock.com\/wp-json\/wp\/v2\/comments?post=34"}],"version-history":[{"count":6,"href":"https:\/\/iplocationblock.com\/wp-json\/wp\/v2\/posts\/34\/revisions"}],"predecessor-version":[{"id":508,"href":"https:\/\/iplocationblock.com\/wp-json\/wp\/v2\/posts\/34\/revisions\/508"}],"wp:attachment":[{"href":"https:\/\/iplocationblock.com\/wp-json\/wp\/v2\/media?parent=34"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/iplocationblock.com\/wp-json\/wp\/v2\/categories?post=34"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/iplocationblock.com\/wp-json\/wp\/v2\/tags?post=34"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}