Only display a Cookie Banner in California or other States.

Get compliant today in the European Union, United States, Canada, United Kingdom, Australia, Brazil & South Africa with the only Privacy Suite for WordPress that offers a fully-featured plugin for Worldwide Compliance.

Complianz determines whether a Cookie Banner should be displayed to a visitor of your website (and whether the ‘opt-in’ or ‘opt-out’ banner should be displayed), based on the country from which they visit your website with GEO IP.

But what if you want to be more specific than the country? For example, let’s say that you only wish to display a Cookie Banner to visitors in a specific State or Province instead.

Complianz Premium uses the MaxMind GeoLite2 Country database (approximately ~6mb in file size) which allows for targeting on a country-specific level. But if the need arises to target more specifically than the visitor’s country, such as on a state/province level, this will require use of the MaxMind GeoLite2 City database instead (~69mb in file size).

This guide details how you can implement the MaxMind GeoLite2 City database with the “Geolocation IP Detection” plugin by YellowTree to determine the visitor’s state/province, and how to hide/display the Cookie Banner based on this.

We recommend only using this CSS-based approach if the below applies to your site:

  • The site only targets opt-out regions such as the United States.
  • You wish to display the Cookie Banner in one specific state only. It will be hidden in all others.
  • The site does not use page caching (which results in the Geolocation PHP code running for the first visitor only and caching that output, resulting in unexpected behavior for other visitors).

Configuring the Geolocation plugin

  1. Navigate to Plugins > Add New and search for “Geolocation”, find the Geolocation IP Detection plugin by Yellow Tree (Benjamin Pick) and activate it on your website.
  2. Navigate to Settings > Geolocation IP Detection and set the “Data Source” to “Maxmind GeoIP Lite City (Automatic download & update)”. We recommend leaving this Settings page open in a separate tab during completion of the next steps.
  3. Navigate to the MaxMind site by clicking here, and then click the “Sign up for GeoLite2” button to create an account and access the GeoLite2 database.
  4. Once logged in to the MaxMind site, navigate to your MaxMind account page by clicking “My Account” in the top navigation bar (visible when hovering over the user/account icon).
  5. Select “Account Information” from the left-hand side menu. Copy your Account ID from the page and place it in the “Account ID” field in the Geolocation plugin (under “Options for this data source”).
  6. Back on the MaxMind site, select “Manage License Keys” from the left-hand side menu and click “Generate a new license key” to generate a license. Copy the key, place it in the License key field in the Geolocation plugin, and Save the settings. The configuration of the Geolocation plugin under “Options for this data source” should now look like this:
  7. Scroll down to the “General Options” section of the Geolocation settings page, enable the setting to “Add a country-specific CSS class to the <body>-Tag on every page” and Save the settings.

Adding the CSS

If your configuration is correct, you will now find that the Geolocation plugin inserts classes in the <body> element of your website based on the visitor’s location. We can now use these body classes to toggle the Cookie Banner based on if these classes are present (or not).

In the above example image, the visitor reached the site from an IP address in New Jersey. This is indicated by the “geoip-province-NJ” class.

To hide the Cookie Banner in all states except for New Jersey, you can implement the below CSS under Appearance > Customize > Additional CSS.

body:not(.geoip-province-NJ) .cmplz-cookiebanner {

Change the “.geoip-province-NJ” class between the brackets to the class of your desired state/province where the Cookie Banner should exclusively be displayed.



Recent articles

The Privacy Suite for WordPress
Get compliant today with the only Privacy Suite made for WordPress


Popular articles

Join our mailing list - 8 Tips & Tricks in your inbox over the next 8 weeks!