Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save pbrocks/e0801cd1dc3ac1e5c8d9a56e6ab0a104 to your computer and use it in GitHub Desktop.
Save pbrocks/e0801cd1dc3ac1e5c8d9a56e6ab0a104 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 );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment