Last active
May 20, 2018 05:02
-
-
Save soderlind/8e2b759e66f8cb178665745c61d72c5d to your computer and use it in GitHub Desktop.
GDPR: Anonymize WordPress user IP address in comments, supports IP4 and IP6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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' ) ); | |
} | |
} ); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 | |
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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' = '';