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 */
$xffaddrs = explode(',',$_SERVER['HTTP_X_FORWARDED_FOR']);
$_SERVER['REMOTE_ADDR'] = $xffaddrs;
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.
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 */
$_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.
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.