WordPress がコメント作成者に表示する IP アドレスを 127.0.0.1 とする問題を修正しました。

このチュートリアルでは、WordPress がコメントの作成者に、実際の IP アドレスではなくローカルホストの IP アドレス (127.0.0.1) を表示する問題を取り上げます。

まず、問題の原因を診断し、それを修正するためのステップバイステップの解決策を提供します。また、セキュリティとモデレーションのために、正確なIPアドレスを追跡することの重要性についても説明します。

このチュートリアルが終わるころには、問題を解決し、WordPress ウェブサイトのコメント投稿者の IP アドレスを正確に追跡できるようになっていることでしょう。この問題に遭遇し、解決策をお探しの方には必読の内容です。

さて、Varnish-Caching-Systemをお使いでしょうか?カッコイイ! そのため、訪問者の本当のIPを見ることができないのです。ご心配なく。ここに完全な解決策があります。

comment author ip

この問題を解決するために

  1. FTP/SFTPにログインする
  2. public_htmlフォルダーに移動します。
  3. wp-config.phpを編集します。
  4. 以下のコードを /* That's all, stop editing! Happy blogging. */
  5. 変更を保存します。
/** 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]; 
}

マーク:この小さな問題は、私がCloudwaysのウェブホスティングで直面していたものです。

代替ケース

同様に、Cloudflareを使用した後、IPが正しく表示されない場合は、以下のように修正します。

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

また、LEMP スタックを root 権限で使用している場合は、Server Block に以下のルールを追加してください。設定ファイルは、/etc/nginx/sites-available/ディレクトリにあります。

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;
...

もし、wp-config.phpファイルを編集するのが面倒な場合は、Code Snippetsプラグインを使用して上記のスニペットを追加してください。これでうまくいくでしょう。このガイドが正しい方向に役立つことを願っています。ありがとうございました。

コメントする

「WordPress がコメント作成者に表示する IP アドレスを 127.0.0.1 とする問題を修正しました。」への18件のフィードバック

  1. 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?

    返信
    • 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');
      返信
  2. Thank you Gulshan. I thought someone is commenting from my server itself. It looks like this problem exists in Cloudways.

    返信
  3. 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.

    返信
  4. 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.

    返信
    • Hi,
      Thanks for coming back.

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

      返信