-
-
Save pbrocks/e0801cd1dc3ac1e5c8d9a56e6ab0a104 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