WordPress comment_author_IP() Function

Beginner
4 min read
Updated: Nov 2026
3 examples
Template tag

Definition

In WordPress, comment_author_IP() prints the IP address that was used when the comment was sent.

It prints the IP as plain text. It helps moderators find spam. It is not meant for public pages.

Syntax

Call it inside the comment loop, or pass a comment ID. The PHP name has a capital IP.

WordPress
comment_author_IP();
comment_author_IP( 15 );
CallWhat it does
comment_author_IP()Prints the IP of the current comment author.
comment_author_IP( 15 )Prints the IP of comment 15.
get_comment_author_IP()Returns the IP instead of printing it.

The tutorial URL uses lowercase letters, but the PHP function name is comment_author_IP().

Full declaration
WordPress
comment_author_IP( int|WP_Comment $comment_id = 0 ): void

Parameters

ParameterDescription
$comment_idOptional. A comment ID or a WP_Comment object. Default 0, which means the current comment.

Return Value

Return ValueDescription
voidNothing is returned. The function prints the IP address.

WordPress Examples

Three short theme examples. Each one has a sample output.

Example 1 — Basic use

Print the IP address of a comment.

WordPress
<p>IP: <?php comment_author_IP(); ?></p>

How It Works

  1. The function prints the saved IP address.
  2. It reads the current comment.
  3. The output is plain text.

Example 2 — Only for moderators

Show the IP only to people who can moderate comments.

WordPress
<?php if ( current_user_can( 'moderate_comments' ) ) : ?>
  <small>IP: <?php comment_author_IP(); ?></small>
<?php endif; ?>

How It Works

  1. current_user_can( 'moderate_comments' ) checks the role.
  2. Only moderators see the IP.
  3. Everyone else sees nothing.

Example 3 — Return value

Use get_comment_author_IP() to work with the value.

WordPress
<?php $ip = get_comment_author_IP(); ?>
<code><?php echo esc_html( $ip ); ?></code>

How It Works

  1. get_comment_author_IP() returns the IP as a string.
  2. esc_html() makes it safe to print.
  3. You can also check or shorten the value before you print it.

Frequently Asked Questions

It prints the IP address that the commenter used.
That is how WordPress names it. Use comment_author_IP() in your code.
No. Show it only to moderators or in the admin area.
Use get_comment_author_IP().
Yes. A visitor can use a proxy or VPN, so the IP is only a hint.

Next: comment_author_link()

Print the commenter's name as a link to their website.

comment_author_link() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful