Using WordPress ‘pre_comment_user_ip’ PHP filter

The pre_comment_user_ip filter allows you to modify the comment author’s IP address before it is saved to the WordPress database.

Usage

add_filter('pre_comment_user_ip', 'your_custom_function_name');
function your_custom_function_name($comment_author_ip) {
    // Your custom code here

    return $comment_author_ip;
}

Parameters

  • $comment_author_ip (string): The comment author’s IP address.

More information

See WordPress Developer Resources: pre_comment_user_ip

Examples

Remove the last IP in X-Forwarded-For header

In this example, we will remove the last IP address from the X-Forwarded-For header.

add_filter('pre_comment_user_ip', 'remove_last_ip_x_forwarded_for');
function remove_last_ip_x_forwarded_for($comment_author_ip) {
    if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
        $ip_list = explode(', ', $_SERVER['HTTP_X_FORWARDED_FOR']);
        array_pop($ip_list);
        $comment_author_ip = implode(', ', $ip_list);
    }
    return $comment_author_ip;
}

Use the first IP in X-Forwarded-For header

In this example, we will use the first IP address from the X-Forwarded-For header as the comment author’s IP.

add_filter('pre_comment_user_ip', 'use_first_ip_x_forwarded_for');
function use_first_ip_x_forwarded_for($comment_author_ip) {
    if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
        $ip_list = explode(', ', $_SERVER['HTTP_X_FORWARDED_FOR']);
        $comment_author_ip = $ip_list[0];
    }
    return $comment_author_ip;
}

Use a custom function to retrieve the client’s IP

In this example, we will use a custom function to retrieve the client’s IP address from different server variables.

add_filter('pre_comment_user_ip', 'get_client_ip');
function get_client_ip($comment_author_ip) {
    $ip_sources = array(
        'HTTP_CLIENT_IP',
        'HTTP_X_FORWARDED_FOR',
        'HTTP_X_FORWARDED',
        'HTTP_FORWARDED_FOR',
        'HTTP_FORWARDED',
        'REMOTE_ADDR'
    );

    foreach ($ip_sources as $source) {
        if (isset($_SERVER[$source])) {
            return $_SERVER[$source];
        }
    }

    return $comment_author_ip;
}

Anonymize IP address

In this example, we will anonymize the comment author’s IP address by removing the last octet.

add_filter('pre_comment_user_ip', 'anonymize_ip');
function anonymize_ip($comment_author_ip) {
    $ip_parts = explode('.', $comment_author_ip);
    array_pop($ip_parts);
    $anonymized_ip = implode('.', $ip_parts) . '.0';
    return $anonymized_ip;
}

Use the Forwarded header

In this example, we will use the Forwarded header to obtain the client’s IP address.

add_filter('pre_comment_user_ip', 'use_forwarded_header');
function use_forwarded_header($comment_author_ip) {
    if (isset($_SERVER['HTTP_FORWARDED'])) {
        preg_match('/for=([^;]+)/', $_SERVER['HTTP_FORWARDED'], $matches);
        if (isset($matches[1])) {
            $comment_author_author_ip = $matches[1];
        }
    }
    return $comment_author_ip;
}