Hooks
modal_kit_pro_visitor_address
filter
Pro
PHP
Change the address Pro uses for rate limits and one play per visitor, for proxy setups the constant doesn’t cover.
Pro’s rate limits (the form relay, Results events and game plays) and one play per visitor go by the visitor’s network address. By default that’s the connection’s address (REMOTE_ADDR). Behind a proxy or CDN that doesn’t pass the visitor’s address on, it’s the proxy’s, so every visitor looks like one.
For the usual case, set MODAL_KIT_PRO_CLIENT_IP_HEADER in wp-config.php to the $_SERVER key of the header your proxy sets, such as HTTP_CF_CONNECTING_IP for Cloudflare. Pro then uses the header’s last entry when it’s a valid address. This filter runs after that, for every other setup.
Parameters
Parameter
Description
$address
string
The connection’s address, or the header’s when the constant is set.
Returns
string The address. An IPv6 address is then reduced to its /64 network, because one home or phone is given a whole one.
Examples
Trust a header only from your own proxies
Your load balancers pass the visitor’s address in X-Real-IP. Read it only when the request came from one of them, so nobody can send it from outside.
add_filter( 'modal_kit_pro_visitor_address', function ( $address ) {
$proxies = array( '10.0.0.10', '10.0.0.11' );
$remote = isset( $_SERVER['REMOTE_ADDR'] )
? sanitize_text_field( wp_unslash( $_SERVER['REMOTE_ADDR'] ) )
: '';
if ( ! in_array( $remote, $proxies, true )
|| empty( $_SERVER['HTTP_X_REAL_IP'] ) ) {
return $address;
}
$real = sanitize_text_field( wp_unslash( $_SERVER['HTTP_X_REAL_IP'] ) );
return filter_var( $real, FILTER_VALIDATE_IP ) ? $real : $address;
} );
A CDN in front of a load balancer
The CDN adds the visitor’s address to the end of X-Forwarded-For, then the load balancer adds the CDN’s. The visitor’s is second from the end; anything before it could have come from the visitor. Use this only if the site can’t be reached except through both.
add_filter( 'modal_kit_pro_visitor_address', function ( $address ) {
if ( empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) {
return $address;
}
$list = array_map(
'trim',
explode(
',',
sanitize_text_field( wp_unslash( $_SERVER['HTTP_X_FORWARDED_FOR'] ) )
)
);
$real = count( $list ) >= 2 ? $list[ count( $list ) - 2 ] : '';
return filter_var( $real, FILTER_VALIDATE_IP ) ? $real : $address;
} );
Good to know
Only trust a header your proxy always sets. Anyone can send it to a site that isn’t behind one, and get round the limits.
Pro never stores the address: limits and plays keep a salted hash of it. Don’t log it from here either.
Return a valid address. Pro doesn’t check what comes back, and an empty string would put every visitor in one allowance.
Bot checks send the connection’s own address (REMOTE_ADDR) to the provider, not this one.
Applied in modal-kit-pro/includes/Visitor.php.