# Store Only Shortened IP Addresses for Comments

> Saves commenters' IP addresses with the last part removed (for example 203.0.113.0), which is still enough for spam checks but no longer identifies a person.

- Type: PHP
- Runs: Run everywhere
- Category: [Privacy & Cookies](https://wpide.com/library/category/privacy/)
- Tags: comments, ip-address, gdpr
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/privacy-mask-comment-ip/

## Code

```php
// 'mask' keeps the network part of the address, 'none' stores nothing.
$wpide_comment_ip_mode = 'mask';

add_filter( 'pre_comment_user_ip', function ( $ip ) use ( $wpide_comment_ip_mode ) {
    if ( 'none' === $wpide_comment_ip_mode ) {
        return '';
    }

    return function_exists( 'wp_privacy_anonymize_ip' ) ? wp_privacy_anonymize_ip( $ip ) : '';
}, 20 );
```

## Notes

Set $wpide_comment_ip_mode to 'none' to store no IP address at all; some anti-spam services work less well without it.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Store Only Shortened IP Addresses for Comments" 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/
