Skip to content

Instantly share code, notes, and snippets.

@soderlind
Last active May 20, 2018 05:02
Show Gist options
  • Save soderlind/8e2b759e66f8cb178665745c61d72c5d to your computer and use it in GitHub Desktop.
Save soderlind/8e2b759e66f8cb178665745c61d72c5d to your computer and use it in GitHub Desktop.
GDPR: Anonymize WordPress user IP address in comments, supports IP4 and IP6
<?php
if ( true == apply_filters( 'is_gdpr', true ) ) {
add_filter( 'pre_comment_user_ip', function( $ip ) {
$packed_in_addr = inet_pton( $ip );
if ( 4 == strlen( $packed_in_addr ) ) {
return inet_ntop( inet_pton( $ip ) & inet_pton( '255.255.0.0' ) );
} else {
return inet_ntop( inet_pton( $ip ) & inet_pton( 'ffff:ffff:ffff:ffff:0000:0000:0000:0000' ) );
}
} );
}
<?php
add_filter( 'pre_comment_user_ip', function( $ip ) {
return inet_ntop( inet_pton( $ip ) & inet_pton( '255.255.0.0' ) ); // return an IP address in the form of nnn.nnn.0.0
} );
<?php
// using the new wp_privacy_anonymize_ip function, available in WordPress 4.9.6
if ( function_exists( 'wp_privacy_anonymize_ip' ) ) {
add_filter( 'pre_comment_user_ip', function( $ip ) {
return wp_privacy_anonymize_ip( $ip );
}
}
@soderlind
Copy link
Author

Note, doesn't fix existing user IP for existing comments. Backup your database and run something like this on it:
UPDATE 'wp_comments' SET 'comment_author_IP' = '';

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment