Instead, WordPress displays the localhost IP address 127.0.0.1. Depending on the software you installed on your server and how it is configured, the client IP address may not be forwarded properly. Unfortunately, WordPress has no built-in option to forward the client IP address. Here’s how to fix an incorrect IP address in the WordPress comments.

Fix an Incorrect IP Address in WordPress

Note: before editing any file in WordPress, please create a good backup of that file.The backup helps you restore the file in the event of any mishaps. The easiest way to solve this problem is by adding a simple code snippet to the “wp-config.php” file. To do that open your FTP client, log into your website’s FTP account and open the wp-config.php file.

Once the file has been opened, copy the below code snippet and paste it at the bottom of the file. Next, save the file and re-upload it.

That’s it, you should see the real client IP address in the WordPress comments page and elsewhere.

What’s Happening With that Code Snippet?

When your WordPress website is behind an HTTP proxy or using a load balancer, an HTTP header called “X-Forwarded-For” is used to store all the IP addresses including the real client IP address in the chain. By default, the IP addresses in the “X-Forwarded-For” HTTP header are comma separated and the first IP address in the chain is always the client IP address. What we are doing with the above code snippet is taking all those IP addresses, exploding them into individual pieces and storing them in the $mte_xffaddrs array. Since the first IP address is related to the client, we can use the zero index and point it to REMOTE_ADDR within the $_SERVER array.

Fix Incorrect IP Address in WordPress Using Plugin

If you don’t want to mess with core WordPress files, then you can use a plugin called Proxy Real IP. Though the plugin hasn’t been updated in a long while, it is still functional. The plugin basically does the same thing as the above code. However, the plugin uses the preg_match function rather than the explode function. Just install and activate the plugin like any other. There is no settings page or options to configure.

Fix Incorrect IP When Using Cloudflare

When you are behind a proxy like Cloudflare, WordPress may sometimes show the Cloudflare IP rather than the actual client IP or even the localhost IP. There is an easy fix for this issue, too. Just paste the below code at the bottom of the wp-config.php file. With the above code we are grabbing the real client IP using HTTP_CF_CONNECTING_IP and pointing REMOTE_ADDR to that IP address. Comment below sharing your thoughts and experiences about using the above methods to fix an incorrect IP address in the WordPress comments.