How to manage access to WordPress website based on location

This article is dedicated to premium IP Check add-on 4.1.0 or higher that is available for purchase from our online store.

Sometimes there is a need to customize a user’s experience based on his/her geographical location. For example, you may want to restrict the ability to see and read a certain group of pages to users that come from North America or an individual country like Ukraine, China, Brazil, etc. IP Check add-on comes with embedded integration to ipstack service that allows you to define the most granular access rules based on user’s geographical positioning.

Note! We chose to integrate with ipstack because it offers very easy-to-use API and first 10k requests per months are free. You have to have at least free-tier account in order to use geo-location capability.

Upon successful installation of the IP Check add-on, the next step is to configure AAM to use ipstask service with ConfigPress. The minimum required option is ipstack.license that contains the license key obtained from the ipstack website. Below is the complete list of all available settings:

  • ipstack.license: ipstack license key. For organizations that manage and deploy secrets separately, AAM also checks AAM_IPSTACK_LICENSE environment variable for the license;
  • ipstack.cache.limit (default 200): internal AAM ring-buffer cache that stores responses from the ipstack service;
  • ipstack.test.ip: for testing purposes only. Provide any valid IP address to emulate a user’s IP address. Do not forget to remove or comment out this option when done testing. Otherwise all users will be associated with provided IP address.;
  • ipstack.schema (default http: The free ipstack tier does not include SSL encryption. If you have at least BASIC plan, change this option to be https;
  • ipstack.fields (default continent_name, country_code, country_name, region_code, region_name, city, zip: The list of fields that ipstack returns based on a users IP address;

From here you have two possible ways to define access rules based on geolocation: with AAM UI and with AAM access policies.

AAM UI

AAM UI offers two ways to define access rules: for the entire website or for any individual content or content type. While the first option is available under “IP Check” tab on the AAM page, the second option can be found on the Posts & Terms tab as REFERENCE CHECK option.

IP Check Whole Website

If you need to protect the entire website, disregarding what page a user is browsing, then you use “IP Check” tab, however, if you need to protect any specific post, page, custom post type, category or group of posts with any specific tag, then you use REFERENCE CHECK option.

Access Policy

Access policy gives you no limit to what you can do with it. You can define the most granular access to any given website resource(s) like a ninja. For example, you can hide some unwanted widgets if a user is coming from North Korea, or restrict access to the entire backend area if a user is not physically located in New York City.

Try! This access policy restricts the ability to leave comments on all posts if a user is not based in US. Policy AAM000024

IP Check add-on extends AAM access policy with new marker IPSTACK that basically invokes ipstack API and fetches geo-information based on a current users IP address. The list of all available properties that can be included in the marker is listed in the official ipstack documentation here.

Note! For performance reasons, by default, AAM fetches only following fields: continent_name, country_code, country_name, region_code, region_name, city, zip. This is configurable value with ipstack.fields ConfigPress option that accepts comma-separated list of ipstack fields.

From here you can define conditional statements that will be applied to a current user if conditions are met. In the example below, we restrict user registration, if visitor is not coming from Europian country, however, also allows San Francisco city.

{
    "Version": "1.0.0",
    "Dependency": {
        "wordpress": ">=5.2.4",
        "advanced-access-manager": ">=6.3.3",
        "${CONST.AAM_IP_CHECK}": {
            "Name": "IP Check",
            "Version": ">=4.1.0",
            "URL": "https://aamplugin.com/pricing/ip-check"
        }
    },
    "Param": [
        {
            "Key": "option:users_can_register",
            "Value": 0,
            "Condition": {
                "NotEquals": {
                    "${IPSTACK.continent_name}": "Europe",
                    "${IPSTACK.city}": "San Francisco"
                }
            }
        }
    ]
}

Get notified about important updates and new features (no more than one email per month).