How to block countries in Gravity Forms?

Gravity Forms

Gravity Forms doesn’t have a built-in feature to block submissions by country or IP address, but there are effective ways to achieve country-level restrictions. Whether you want to limit the countries users can select in your forms or block spam entirely, this guide covers it all.

No Built-In Country Blocking in Gravity Forms

Gravity Forms does not natively support country-based submission blocking. However, you can customize its Country Select field using filters or use third-party tools like OOPSpam Anti-Spam for more comprehensive blocking. If you need broader site-wide control, Cloudflare can help you block access from entire regions.

We’ll go over both methods:

1. Block Countries in Gravity Forms Using Code

One of Gravity Forms’ features is its ability to be customized through hooks and filters. With a small code snippet, you can add or remove countries from the dropdown list of your Country field.

Using the gform_countries Filter

You can edit your form’s country list by adding code to your functions.php file (in your active theme) or within a custom plugin.

Example: Removing a Country

To remove “United States” from the list of countries, you can use:

add_filter( 'gform_countries', 'remove_country' );

function remove_country( $countries ) {

    $key = array_search( 'United States', $countries );

    unset( $countries[ $key ] );

    return $countries;

}

This code uses array_search to find and remove the country from the list.

Example: Adding a Country

To add a custom country to the list, use:

add_filter( 'gform_countries', 'add_country' );

function add_country( $countries ) {

    $countries[] = 'Custom Country';

    sort( $countries );

    return $countries;

}

Limiting Countries in a Single Form

To restrict countries for one specific form (e.g., form ID 77), you can use form-specific filters like:

Replace 77 with the actual form ID.

2. Block Spam and Countries with OOPSpam Anti-Spam

While filters are useful for customizing dropdowns, they won’t stop spam submissions from bots or unwanted countries. This is where OOPSpam Anti-Spam comes in. It integrates seamlessly with Gravity Forms and offers real-time spam detection, country filtering, and detailed logs.

What OOPSpam Adds to Gravity Forms

How to Set It Up

Step 1: Install the Plugin

Go to Plugins > Add New, search for OOPSpam Anti-Spam, install, and activate it.

OOPSpam Anti-Spam dashboard

Step 2: Connect Your API Key

Create an account at OOPSpam.com, generate an API key, and paste it into OOPSpam Anti-Spam > Settings in your WordPress dashboard.

Connect Your API Key

Step 3: Enable Gravity Forms Protection

Scroll to the Gravity Forms section and toggle spam protection on.

Enable Gravity Forms Protection

Step 4: Set Up Country Blocking

Use the dropdown settings to either:

Set Up Country Blocking

Save the settings, and OOPSpam will handle the rest.

Reviewing Blocked Submissions

OOPSpam offers transparent spam logs that include:

You can review blocked and accepted entries directly in your WordPress dashboard:

WordPress dashboard

or in the OOPSpam dashboard:

OOPSpam dashboard logs

3. Block Entire Countries with Cloudflare

If you want to stop all traffic from specific countries not just form entries—you can do this at the network level with Cloudflare’s Web Application Firewall (WAF).

Important: This method blocks the entire site for visitors from certain regions.

Cloudflare

How to Set Up Country Blocking in Cloudflare

How to Set Up Country Blocking in Cloudflare

  1. Log in to your Cloudflare account.
  2. Select your website.
  3. Navigate to Security > WAF > Firewall Rules.
  4. Click Create Firewall Rule.
  5. Name the rule (e.g., “Block Countries”).
  6. Set conditions:
    • Field: Country

    • Operator: is in

    • Value: Select the countries to block.

  7. Action: Block and save the rule.

When to Use This

Final Thoughts

Gravity Forms doesn’t have native country-blocking, but you can:

With these methods, you can keep your forms clean and secure while still providing a smooth user experience.

Spam Protection for WordPress, Zapier, Make and more.

Since our launch in 2017 we’ve been perfecting our API to be the trusted option for small businesses to enterprise— and continue to stick to our values of being the accessibility and privacy-friendly option. Give us a shot!

Try OOPSpam for free → Try our WordPress plugin for free →

✓ No credit card required ✓ Cancel anytime

Enjoy Reading This Article?

Here are some more articles you might like to read next: