Halloween Sale 15% off Ends in See the plans
PHP

Block IP Addresses From WPForms

Rejects WPForms submissions from IP addresses or ranges you list, such as a persistent spammer, while they can still read the site.

Don’t have WPIDE yet? Install it on your site

Code

$wpide_wpf_blocked_ips = array( '203.0.113.7', '198.51.100.' );

add_filter( 'wpforms_process_honeypot', function ( $honeypot ) use ( $wpide_wpf_blocked_ips ) {
    if ( $honeypot ) {
        return $honeypot;
    }

    $ip = isset( $_SERVER['REMOTE_ADDR'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REMOTE_ADDR'] ) ) : '';

    foreach ( $wpide_wpf_blocked_ips as $blocked ) {
        $prefix = in_array( substr( $blocked, -1 ), array( '.', ':' ), true );
        if ( $ip === $blocked || ( $prefix && 0 === strpos( $ip, $blocked ) ) ) {
            return 'Submissions from your network are not accepted.';
        }
    }

    return $honeypot;
} );

Notes

Use exact addresses, or prefixes ending in a dot or colon for ranges.

How to add this snippet

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.