WordPressのスパムコメントを忘れよう 😎

ダウンロード: Forget Spam Comment Plugin

WordPressのデフォルトのコメントシステム専用の無料アンチスパムプラグインです。

仕組み

また、上級者向けには、以下のようなマニュアル方式もあります。

WordPressのデフォルトのコメントシステムには、2つの大きな問題があります。

  1. スパムコメントを誘引する
  2. コメント投稿者にフォローアップメールを送っていない(しばらく検討します)。

その解決策についてお話ししましょう。

スパムコメントの防止

誰でも/wp-comments-post.phpPOSTリクエストができるようにする代わりに、スパムコメントを100%防ぐためのロジックを追加することができます。

ステップ1.クエリパラメータでコメントのPOSTリクエストのパスを制限する

今回は3つの方法を紹介しますが、1つの方法を使ってください。

Apache ウェブサーバー

  • Yoast > 次のページへ Tools > File Editor
  • RankMath > 次のページへGo to General Settings > Edit .htaccess
  • FTP/SSH > チェック /var/www/html
# If Query string doesn't matches return 404
<IfModule mod_rewrite.c>
	RewriteEngine On
        RewriteCond %{REQUEST_URI} .wp-comments-post\.php
        # You may change 45jpfAY9RcNeFP to something else
        RewriteCond %{QUERY_STRING} !^45jpfAY9RcNeFP
	RewriteRule (.*) - [R=404,L]
</IfModule>

LiteSpeedをお使いの方は、.htaccessファイルにも対応しています。導入後、再起動が必要です。

NGINX

location = /wp-comments-post.php {

 if ($query_string !~ "45jpfAY9RcNeFP") {
     return 404;
  }
}

Cloudflare

prevent spam comments
  • Cloudflare Dashboard にログインする
  • Firewall > Firewall Rules
  • 以下の式で新しいファイアウォールルールを作成します。
FieldOperatorValue
URIcontainswp-comments-post.phpAnd
URL Query Stringdoes not equal45jpfAY9RcNeFP
  • Choose Action: Block

最後には、表情が見える

(http.request.uri contains "wp-comments-post.php" and http.request.uri.query ne "45jpfAY9RcNeFP")

ステップ2. ScrollイベントのComment POST URLを修正する

  • Code Snippetsプラグインまたはテーマのfunctions.phpを使用して、以下の関数を追加します。
  • ドメインとフォームIDは必ず正しくお使いください。
function correct_comment_url_on_scroll() {
// Check if Comment is enabled
if(comments_open()) echo '<script>
let commentForm = document.querySelector("#commentform, #ast-commentform, #ht-commentform");

// Load new comment path on the scroll event
document.onscroll = function () {
    commentForm.action = "https://www.example.com/wp-comments-post.php?45jpfAY9RcNeFP";
};
</script>';
}
add_action('wp_footer', 'correct_comment_url_on_scroll', 99);

また、GeneratePressプレミアムテーマユーザーであれば、Elementsモジュールを使って上記のJSパーツを直接追加することができます。

<script>
let commentForm = document.querySelector("#commentform");

commentForm.action = "https://www.example.com/wp-comments-post.php";
// Load new comment path on the scroll event
document.onscroll = function () {
    commentForm.action = "https://www.example.com/wp-comments-post.php?45jpfAY9RcNeFP";
};
</script>
  • Add a new hook
  • New Hook Title: Change Comment URL on the Fly
  • Hook: WP Footer
  • Priority: 99
  • Location: Posts – All posts
  • Publish

動作しているかどうかを確認する方法は?

  • 通常、WordPressは/wp-comments-post.phpのGETリクエストに対して405のレスポンスを返します。
  • しかし、上記の設定の後、Access Deniedと表示されるはずです。
  • また、スパムコメントを防ぐために追加した特別なクエリストリング45jpfAY9RcNeFPが存在する場合にのみURLを読み込むようにします。このクエリ文字列は、上記のすべての設定において、他のものに変更することができます。
  • ソースコードには通常 wp-comments-post.php のパスが表示されますが、スクロール後に要素を調べると、クエリ文字列が追加されていることに気がつきます。

結果その1:スパムがない

no spam

Result #2: Spammers getting blocked

log

デフォルトのコメントシステムをより良くするためのボーナスアドバイス

  • WordPressのコメントシステムは、デフォルトではコメント作成者にフォローアップのメールを送信しません。この問題を解決するには、Arno Welzel氏のComment Reply Email Notificationプラグインを使用することができます。

この情報を気に入っていただけたなら、ぜひお友達にも教えてあげてください。🙏

コメントする

「WordPressのスパムコメントを忘れよう 😎」への20件のフィードバック

  1. Thank you so much Sir Gulshan Kumar! I really appreciate your guide, and this has saved me tons of headache. Implemented via Cloudflare WAF plus Generatepress Elements Module.

    返信
  2. By the way, how do you know if legit users won’t get blocked? Before this, I’ve been using a CF firewall rules that will blocks or challenge anyone who visit wp-comments-post.php but actually by doing so, legit users also got blocked. How this one different? Is it because of the string “45jpfAY9RcNeFP” that will differentiate which request is from bot and which request is from legit users? If legit users, they should’ve the string, if it’s spam, they don’t have that string. Am I understand it correctly?

    返信
  3. Does this plugin / method will stop website from having spam comments once and for all or do we still receive it but it’s automatically filtered from the real comments (like Akismet)?

    返信
  4. Hi Gulshan.

    I just wanted to let you know that I’ve been using your plugin for a few weeks now and it is brilliant. Genuine comments still get through but all of the spam, and I mean ALL of it, is filtered out. Excellent job!

    Well done and thank you so much for sharing it for free so that hobby bloggers like me can make use of it.

    返信
    • Hi,
      I am also using plugin at this blog. It works perfectly fine here.
      If you want to me look into this issue, please provide your actual site URL where I can see live.
      Thanks!

      返信
    • You can use MD5 generator or write any random text. I’d recommend using plugin for set and forget experience.

      返信