Fix – WordPress Showing Localhost IP(127.0.0.1) for Comment Author Instead of Real

This tutorial will address a problem where WordPress is displaying the local host IP address (127.0.0.1) for the author of a comment instead of its actual IP address.

We will first diagnose the cause of the problem and then provide a step-by-step solution to fix it. We will also discuss the importance of accurate IP address tracking for security and moderation purposes.

By the end of this tutorial, you will have solved the problem and be able to accurately track the IP addresses of commenters on your WordPress website. This is a must-read for anyone experiencing this problem and looking for a solution.

So, you are using Varnish? Cool! That’s why you are unable to see real IP of your visitors. No worries, here’s the complete solution.

comment author ip

To fix this problem,

  1. Login to FTP/SFTP
  2. Go to public_html folder
  3. Edit wp-config.php
  4. Add below code just before /* That’s all, stop editing! Happy blogging. */
  5. Save Changes.
/** Required Code for showing correct IP address */
if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])) { 
$xffaddrs = explode(',',$_SERVER['HTTP_X_FORWARDED_FOR']); 
$_SERVER['REMOTE_ADDR'] = $xffaddrs[0]; 
}

Mark: This little problem I had faced on Cloudways web hosting.

Alternative case

Similarly, if your IP is showing incorrect after using Cloudflare, here’s the fix –

/** Correct IP in case of Cloudflare **/
if(array_key_exists('HTTP_CF_CONNECTING_IP', $_SERVER)){ 
 $_SERVER['REMOTE_ADDR'] = $_SERVER['HTTP_CF_CONNECTING_IP']; 
}

Alternatively, if you have LEMP stack with root access, then add below rule under Server Block. You can find your configuration file at /etc/nginx/sites-available/ directory.

server {
root /var/www/html;
index index.php index.html index.htm;
server_name .example.com;
# Get real IP
set_real_ip_from 0.0.0.0/0;
set_real_ip_from ::/0;
real_ip_header X-Forwarded-For;
...

In case, if you don’t feel comfort in editing wp-config.php file, add above snippet using Code Snippets plugin. It will work fine. I hope this guide helps in the right direction. Thanks

Leave a Comment

18 thoughts on “Fix – WordPress Showing Localhost IP(127.0.0.1) for Comment Author Instead of Real”

  1. Avatar of sabhay
    sabhay

    Time it shows is of the server not the user. Is it possible to check user time as well?

    Reply
  2. Avatar of Aquif Shaikh
    Aquif Shaikh

    That’s the exact problem I was facing. The Cloudways support did rectify it twice but they never shared the solution. It was Mustasaam who referred me to this page.

    The solution works perfectly for me. Thank you for sharing.

    Just one more query, how did you hide the Website option from the comments box? Most of my readers are internet noobs who feel filling out the Website section is mandatory. I too want to get the same removed. Mind sharing an article on my email ID?

    Reply
    • Avatar of Gulshan Kumar
      Gulshan Kumar

      Thanks for visiting.

      To hide website field, you can use below snippet.

      // Remove URL Field
      function remove_url_comments($fields) {
      unset($fields['url']);
      return $fields;
      }
      add_filter('comment_form_default_fields','remove_url_comments');
      Reply
  3. Avatar of Abhay Prakash
    Abhay Prakash

    I tried both of method mentioned here but still, I got same error plz help me 🙁

    Reply
  4. Avatar of Guilherme
    Guilherme

    Thanks. Thats exactly what I was looking for. Also using and loving Cloudways! 🙂

    Reply
    • Avatar of Gulshan Kumar
      Gulshan Kumar

      Hi dear, Thanks for the kind comment. I am glad to hear from you. 🙂

      Reply
  5. Avatar of EMAURYAA
    EMAURYAA

    Hi Gulshan

    Nice Post about , error of local host in wordpress keep it up

    Reply
  6. Avatar of pushpendra singh
    pushpendra singh

    nice artical sir, very usefull your post thanks for information provide

    Reply
    • Avatar of Gulshan Kumar
      Gulshan Kumar

      Hi Pushpendra, Thank you so much for the kind comment. I am so glad you found it useful.

      Reply
  7. Avatar of Abhishek
    Abhishek

    Thank you Gulshan. I thought someone is commenting from my server itself. It looks like this problem exists in Cloudways.

    Reply
    • Avatar of Gulshan Kumar
      Gulshan Kumar

      You’re welcome! This problem exist in the Cloudways web hosting. Not a big issue though.

      Reply
  8. Avatar of tirupati
    tirupati

    I was looking for the same. It is showing 127.0.0.1. Fixed it. your blog seems very handy for wordpress fixes/optimizations. thanks.

    Reply
    • Avatar of Gulshan Kumar
      Gulshan Kumar

      Hi Tirupati,
      I am so glad you found my blog. Thank you so much.

      Reply
  9. Avatar of Arnab Satapathi
    Arnab Satapathi

    Hi, I’ve found your blog from shoutmeloud forum, and it’s great for optimizing wordpress.

    What plugin/code you’re using for the responsive mobile menu at the left corner of the header? I need one badly.

    Reply
  10. Avatar of Santosh
    Santosh

    Where should I put it Gulshan. Right after <?php opening or /** which is already been there?

    Reply
    • Avatar of Gulshan Kumar
      Gulshan Kumar

      Hi,
      Thanks for coming back.

      You need to put code just before this line
      /* That's all, stop editing! Happy blogging. */

      Reply
  11. Avatar of Chala
    Chala

    Awesome Gulshan. Was having the same issue with the local ip address showing instead of the real ip address.

    Reply
    • Avatar of Gulshan Kumar
      Gulshan Kumar

      Glad to know you found it. Thanks

      Reply