Comment Author IP Address Showing Wrong on WordPress [Fixed]

Recently, I noticed on our blog post comments. I saw something is going wrong with the comment system of WordPress, the comment author’s IP address was wrong.

Incorrect WordPress Comment Author IP

I researched Google and find out in some technical cases WordPress also shows the localhost IP (127.0.0.1) address. If your WordPress website is showing, incorrect or localhost IP addresses of the comment author, then this article will help you to fix this problem. There are two cases, to show incorrect and localhost IP addresses of the comment author.

First, it depends on the server configuration. If you are using varnish on a WordPress site then it is normal to show a local IP address instead of the client’s real IP.

In the second case, If you are using Cloudflare CDN for performance or Ezoic ad-network to monetize the website, then WordPress shows incorrect IP addresses of the comment author. It shows the CDN’s IP address instead of the client’s real IP address.

Fix Localhost IP Address of Comment Author in WordPress

Note: Before proceeding with the steps, we recommend taking a complete backup of your site. Anything goes wrong, you can restore it anytime.

Step – 1. Connect to your server using FTP/SFTP/SSH

Step – 2. Go to public_html or the WordPress installation directory

Step – 3. Edit wp-config.php

Step – 4. Add these lines of code before /* That’s all, stop editing! Happy blogging. */

/** Code for showing correct IP address of comment author */ 
if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])) { 
$xffaddrs = explode(',',$_SERVER['HTTP_X_FORWARDED_FOR']); 
$_SERVER['REMOTE_ADDR'] = $xffaddrs[0]; 
}

Step – 5. Save Changes.

Fix Incorrect IP Address while Using Cloudflare or Ezoic CDN

As I have mentioned above and noticed that the comments author’s IP was showing incorrect. I checked the location of these IP addresses using the KeyCDN IP locator, and the result was different but the IP’s service provider was the same.

Must read: How to Fix Imagick Missing Module on WordPress

On my site, I am using the Ezoic ad-network and Ezoic uses CDN networks, so when visitors comment on the content it shows the CDN’s IP address instead of real IP, if you are using Cloudflare, the problems will be the same.

So, here are some lines of codes, for fixing the incorrect IP address for the WordPress comment author. You must add code in the wp-config.php file, in the same procedure as mentioned above.

/* Show Correct IP in case of Cloudflare or Ezoic */
if(array_key_exists('HTTP_CF_CONNECTING_IP', $_SERVER)){ 
	$_SERVER['REMOTE_ADDR'] = $_SERVER['HTTP_CF_CONNECTING_IP']; 
   }
Show Correct IP Cloudflare in case of Cloudflare or Ezoic

Show Correct IP Address of Comment Author Using Plugin

There are some plugins, you can use one of them to show the correct IP address of the comment author or visitors. A plugin like Real Proxy IP and Real IP Detector uses the same method, that we have mentioned above.

Particularly I found another plugin Real IP and Geo for Cloudflare, this plugin shows geo-location with the client’s real IP and the Cloudflare proxy IP.

Real IP and Geo for Cloudflare

If you use this plugin, it shows more details compared to others, even from the custom code methods. But this plugin has a negative point, in my opinion, this plugin doesn’t store the records on the database, which means if you uninstalled the plugin it will remove the geo and client’s real IP address. On but other hands if you use Real Proxy IP or Real IP Detector, it will store the IP address on the database.

Must read: How to Fix mbstring Missing PHP Module on WordPress

Conclusion

Using Cloudflare CDN or any CDN services for your WordPress website, then you might have this IP related issue. If you have monetized your website with the Ezoic ad network, then you will also be facing the same issue.

When you use these services, the client’s IP addresses are not forwarded to WordPress properly and show the proxy IP addresses, and that is technically incorrect.

So, using any of the methods you can fix the IP issue on WordPress, and if you are still facing the issue, then don’t forget to comment.


Sign up for the newsletter and get our latest tutorials straight to your inbox.

We’ll never share your email address or spam you.

5 thoughts on “Comment Author IP Address Showing Wrong on WordPress [Fixed]”

    • Thank you Shubham Bhai for the comment. Using CDN or any proxy servers on the Website is helpful for protection and performance but, lack of proper configuration WordPress can’t detect the real IP address.

      Reply
  1. Thanks for the article. Looking for more info on:

    – showing IP address in comment list admin screen
    – showing commenter’s IP address in the admin comment list when an admin has marked a comment as ‘spam’. (The display seems to show the IP address of the admin, not the commentor.)
    – techniques for blocking access to proxy users (a favorite technique of ‘trolls’)

    Thanks..

    Reply

Leave a Comment