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.
I researched Google and find out that 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 the Localhost IP Address of the 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 the real IP, if you are using Cloudflare, the problems will be the same.
So, here are some lines of code, 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 the Correct IP Address of the Comment Author Using the Plugin
There are some plugins, you can use one of them to show the correct IP address of the comment author or visitors. 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.
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 the client’s real IP address. On but other hand 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.
Thanks Pronay, Your article is Really helpful for me, I was also facing this issue on my website.
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.
hi, is there a chance to manually edit the ip addresses of the comments?
You can use, a plugin to check the IP address, but to change it, I am not sure if it is available or not.
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..
✅ Nice tweak!
In case, if you have the Wordfence security plugin installed. Then it has an inbuilt option in Wordfence → All options → General Wordfence Options