Title: pre_comment_user_ip
Published: April 25, 2014
Last modified: May 20, 2026

---

# apply_filters( ‘pre_comment_user_ip’, string $comment_author_ip )

## In this article

 * [Parameters](https://developer.wordpress.org/reference/hooks/pre_comment_user_ip/?output_format=md#parameters)
 * [More Information](https://developer.wordpress.org/reference/hooks/pre_comment_user_ip/?output_format=md#more-information)
 * [Source](https://developer.wordpress.org/reference/hooks/pre_comment_user_ip/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/hooks/pre_comment_user_ip/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/hooks/pre_comment_user_ip/?output_format=md#changelog)
 * [User Contributed Notes](https://developer.wordpress.org/reference/hooks/pre_comment_user_ip/?output_format=md#user-contributed-notes)

[ Back to top](https://developer.wordpress.org/reference/hooks/pre_comment_user_ip/?output_format=md#wp--skip-link--target)

Filters the comment author’s IP address before it is set.

## 󠀁[Parameters](https://developer.wordpress.org/reference/hooks/pre_comment_user_ip/?output_format=md#parameters)󠁿

 `$comment_author_ip`string

The comment author’s IP address.

## 󠀁[More Information](https://developer.wordpress.org/reference/hooks/pre_comment_user_ip/?output_format=md#more-information)󠁿

With this filter, we can change the comment author’s IP before it’s recorded. Example
use case can be when a client submits a comment through a proxy server.

The general format of the header is:
 `X-Forwarded-For: client1, proxy1, proxy2`

where the value is a comma+space separated list of IP addresses, the left-most being
the original client, and each successive proxy that passed the request adding the
IP address where it received the request from. In this example, the request goes
through the IPs: client1 -> proxy1 -> proxy2 -> proxy3. Proxy3 is not shown in the`
X-Forwarded-For` header here and appears as the remote address of the request.

Since it is easy to forge an `X-Forwarded-For` header, the given information should
be used with care.

`X-Forwarded-For`, `X-Forwarded-By`, and `X-Forwarded-Proto` are non-standard header
fields and in increasing cases, have been superseded by the standard `Forwarded`
header defined in RFC 7239. Example of a `Forwarded` header:
 `Forwarded: for=192.0.2.60;
proto=http;by=203.0.113.43`

## 󠀁[Source](https://developer.wordpress.org/reference/hooks/pre_comment_user_ip/?output_format=md#source)󠁿

    ```php
    $commentdata['comment_author_IP'] = apply_filters( 'pre_comment_user_ip', $commentdata['comment_author_IP'] );
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/comment.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/7.0/src/wp-includes/comment.php#L2236)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.0/src/wp-includes/comment.php#L2236-L2236)

## 󠀁[Related](https://developer.wordpress.org/reference/hooks/pre_comment_user_ip/?output_format=md#related)󠁿

| Used by | Description | 
| [wp_filter_comment()](https://developer.wordpress.org/reference/functions/wp_filter_comment/)`wp-includes/comment.php` |

Filters and sanitizes comment data.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/hooks/pre_comment_user_ip/?output_format=md#changelog)󠁿

| Version | Description | 
| [1.5.0](https://developer.wordpress.org/reference/since/1.5.0/) | Introduced. |

## 󠀁[User Contributed Notes](https://developer.wordpress.org/reference/hooks/pre_comment_user_ip/?output_format=md#user-contributed-notes)󠁿

 1.   [Skip to note 2 content](https://developer.wordpress.org/reference/hooks/pre_comment_user_ip/?output_format=md#comment-content-4830)
 2.    [Steven Lin](https://profiles.wordpress.org/stevenlinx/)  [  5 years ago  ](https://developer.wordpress.org/reference/hooks/pre_comment_user_ip/#comment-4830)
 3.  [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fpre_comment_user_ip%2F%23comment-4830)
     Vote results for this note: 0[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fpre_comment_user_ip%2F%23comment-4830)
 4.  Example Migrated from Codex:
 5.  Use the left-most IP (the original client) in the `X-Forwarded-For` header as 
     the comment author’s IP address.
 6.  Note: You may need to adjust the example below for the standard `Forwarded` header,
     which supersedes the non-standard `X-Forwarded-For` header.
 7.      ```php
         add_filter( 'pre_comment_user_ip', 'auto_reverse_proxy_pre_comment_user_ip');
     
         function auto_reverse_proxy_pre_comment_user_ip()
         {    
         	$REMOTE_ADDR = $_SERVER['REMOTE_ADDR'];
     
         	if (!empty($_SERVER['X_FORWARDED_FOR'])) {
         		$X_FORWARDED_FOR = explode(',', $_SERVER['X_FORWARDED_FOR']);
         		if (!empty($X_FORWARDED_FOR)) {
         			$REMOTE_ADDR = trim($X_FORWARDED_FOR[0]);
         		}
         	}
     
         	/*
         	* Some PHP environments will use the $_SERVER['HTTP_X_FORWARDED_FOR'] 
         	* variable to capture visitor address information.
         	*/
         	elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
         		$HTTP_X_FORWARDED_FOR= explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
         		if (!empty($HTTP_X_FORWARDED_FOR)) {
         			$REMOTE_ADDR = trim($HTTP_X_FORWARDED_FOR[0]);
         		}
         	}
     
         	return preg_replace('/[^0-9a-f:\., ]/si', '', $REMOTE_ADDR);
         }
         ```
     
 8.   * After using the example, the ‘comment_author_IP’ will be changed to current
        admin’s IP after editing the comment from wp-admin. Pls go to this link for
        details: [https://core.trac.wordpress.org/ticket/54482?nocache](https://core.trac.wordpress.org/ticket/54482?nocache)
      * [龙笑天](https://profiles.wordpress.org/lxtx/) [5 years ago](https://developer.wordpress.org/reference/hooks/pre_comment_user_ip/#comment-5494)
 9.   [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fpre_comment_user_ip%2F%3Freplytocom%3D4830%23feedback-editor-4830)

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fpre_comment_user_ip%2F)
before being able to contribute a note or feedback.