# 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.

- Type: PHP
- Runs: Run everywhere
- Category: [Plugin Integrations](https://wpide.com/library/category/integrations/)
- Tags: redirection, privacy, gdpr
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/redirection-anonymize-log-ip/

## Code

```php
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 use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Anonymize IP Addresses in Redirection Logs" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
