However, on some other servers, WordPress might not display the correct IP address in the comment dashboard or other locations. Instead, WordPress shows the localhost IP address as 127.0.0.1. Therefore, in this article, Mytour will guide you on how to fix the WordPress bug displaying localhost IP address in user comments.
Fixing Incorrect IP Address Issue on WordPress
Note: Before editing any files in WordPress, it's advisable to create a backup file to prevent any potential mishaps.
The simplest way to address this issue is by adding a code snippet to the 'wp-config.php' file. To do this, open your FTP client, login to your FTP account, and open the wp-config.php file.
Once the file is open, copy the code snippet below and paste it at the end of the file:
// Code for displaying accurate client IP address
if ( isset( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) {
$mte_xffaddrs = explode( ',', $_SERVER['HTTP_X_FORWARDED_FOR'] );
$_SERVER['REMOTE_ADDR'] = $mte_xffaddrs[0];
}
Then save the file and upload it again.
From now on, you will see the actual client IP address on the WordPress comment page and other locations, indicating that you have successfully fixed the WordPress bug displaying localhost IP address in user comments.
What Happens with Code Snippet?
When the WordPress website is behind an HTTP proxy or using load balancing, HTTP header called 'X-Forwarded-For' is used to store all IP addresses, including the real client IP address, in a string.
By default, the IP addresses in X-Forwarded-For are separated by commas, and the first IP address in the string is always the client's IP address.
What we do with the code snippet above is to retrieve all those IP addresses, split them into separate parts, and store them in the array $mte_xffaddrs. Since the first IP address pertains to the client, we can use the Zero index and point it to REMOTE_ADDR in the $ _SERVER array.
Fixing Incorrect IP Address on WordPress Using Plugin
If you prefer not to touch the WordPress core files, you can use a plugin called Proxy Real IP. Although this plugin hasn't been updated for a long time, it still works quite well. Essentially, this plugin works like the code snippet above. The only difference is that the Proxy Real IP plugin uses the preg_match function instead of the explode function.
Simply install and activate the plugin just like you would with any other plugin. There are no setup pages or configuration options.
Download the Proxy Real IP plugin to your machine and install it here: Download Proxy Real IP
Fixing Incorrect IP Address on WordPress Using Cloudflare
When behind a proxy like Cloudflare, WordPress may sometimes display the Cloudflare IP instead of the actual visitor's IP or the localhost IP address.
There's a simple way to fix this issue. Just paste the following code snippet at the end of the wp-config.php file:
// Correcting IP address when using Cloudflare
if ( array_key_exists( 'HTTP_CF_CONNECTING_IP', $_SERVER ) ) {
$_SERVER['REMOTE_ADDR'] = $_SERVER['HTTP_CF_CONNECTING_IP'];
}
The code snippet above retrieves the real client IP address using HTTP_CF_CONNECTING_IP and only points REMOTE_ADDR to that IP address.
Above, Mytour has just guided you through some ways to fix the WordPress bug displaying localhost IP address in user comments. Are you facing this issue? Don't forget to share your thoughts with Mytour.
If you're exploring WordPress and want to experience this website platform, you can install WordPress on localhost first to get familiar and explore it gradually.
After successfully installing, proceed with the design steps, create your own WordPress according to your preferences, the article on creating WordPress will help you do this more easily, Wish you success!