How to Fix WordPress Showing Localhost IP Address for Comments

Spread the love

On a regular web host, WordPress has no problems displaying the correct IP address of your site visitors, commenters, etc. However, on some servers your WordPress site may not display the correct IP address in the comments dashboard and other places.

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.

// Code for showing correct client IP address
if ( isset( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) { 
    $mte_xffaddrs = explode( ',', $_SERVER['HTTP_X_FORWARDED_FOR'] ); 
    $_SERVER['REMOTE_ADDR'] = $mte_xffaddrs[0]; 
}

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.

// Fix incorrect IP when using Cloudflare
if ( array_key_exists( 'HTTP_CF_CONNECTING_IP', $_SERVER ) ) { 
    $_SERVER['REMOTE_ADDR'] = $_SERVER['HTTP_CF_CONNECTING_IP']; 
}

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.

Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox

Sign up for all newsletters.
By signing up, you agree to our Privacy Policy and European users agree to the data transfer policy. We will not share your data and you can unsubscribe at any time. Subscribe


Vamsi Krishna

Vamsi is a tech and WordPress geek who enjoys writing how-to guides and messing with his computer and software in general. When not writing for MTE, he writes for he shares tips, tricks, and lifehacks on his own blog Stugon.

Comments are closed