Anonymize IP Addresses in Redirection Logs
Masks the last part of visitor IP addresses before the Redirection plugin stores them in its redirect and 404 logs, keeping the logs useful while respecting privacy laws.
Opens WPIDE on
Code
add_filter( 'redirection_request_ip', function ( $ip ) {
if ( ! is_string( $ip ) || '' === $ip ) {
return $ip;
}
if ( filter_var( $ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 ) ) {
return preg_replace( '/\.\d+$/', '.0', $ip );
}
if ( filter_var( $ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6 ) ) {
$parts = explode( ':', $ip );
return implode( ':', array_slice( $parts, 0, 3 ) ) . '::';
}
return $ip;
} );
Notes
IPv4 keeps the first three parts (1.2.3.0); IPv6 keeps the first three groups.
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.