Block IP Addresses From Gravity Forms
Stops Gravity Forms submissions from IP addresses or ranges you list and marks them as spam, without blocking those visitors from reading the site.
Opens WPIDE on
Code
$wpide_gf_blocked_ips = array( '203.0.113.7', '198.51.100.' );
add_filter( 'gform_entry_is_spam', function ( $is_spam, $form, $entry ) use ( $wpide_gf_blocked_ips ) {
if ( $is_spam ) {
return $is_spam;
}
$ip = (string) rgar( $entry, 'ip' );
foreach ( $wpide_gf_blocked_ips as $blocked ) {
$prefix = in_array( substr( $blocked, -1 ), array( '.', ':' ), true );
if ( $ip === $blocked || ( $prefix && 0 === strpos( $ip, $blocked ) ) ) {
return true;
}
}
return false;
}, 10, 3 );
Notes
Use exact addresses or prefixes ending in a dot or colon (198.51.100.) for ranges.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.