Limit Sign-Ups per IP Address
Allows only a few new accounts per IP address per day, which stops bots and people from creating piles of accounts.
Opens WPIDE on
Code
// Accounts allowed per IP address in the window below.
$wpide_reg_ip_limit = 3;
$wpide_reg_ip_window = DAY_IN_SECONDS;
$wpide_reg_ip_key = function () {
$ip = isset( $_SERVER['REMOTE_ADDR'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REMOTE_ADDR'] ) ) : '';
return 'wpide_reg_ip_' . md5( $ip );
};
add_filter( 'registration_errors', function ( $errors ) use ( $wpide_reg_ip_limit, $wpide_reg_ip_key ) {
if ( (int) get_transient( $wpide_reg_ip_key() ) >= $wpide_reg_ip_limit ) {
$errors->add( 'wpide_reg_ip', '<strong>Error:</strong> Too many accounts have been created from your network today. Please try again tomorrow.' );
}
return $errors;
}, 5 );
add_action( 'register_new_user', function () use ( $wpide_reg_ip_window, $wpide_reg_ip_key ) {
$key = $wpide_reg_ip_key();
set_transient( $key, (int) get_transient( $key ) + 1, $wpide_reg_ip_window );
} );
Notes
Behind a proxy or CDN make sure REMOTE_ADDR holds the visitor's real IP, or everyone shares one limit.
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.