Nu kan du glömma skräppostkommentarer i WordPress 😎

Ladda ner: Plugin för att glömma spamkommentarer

Det här är ett gratis insticksprogram mot skräppost som enbart är avsett för WordPress standardkommentarsystem.

Hur fungerar det?

Alternativt, för avancerade användare, är den manuella metoden nedan.

Standardkommentarsystemet i WordPress har två stora problem.

  1. Lockar till sig skräppostkommentarer.
  2. Skicka inget uppföljningsmeddelande till författaren av kommentaren (kommer att diskuteras om ett tag).

Låt oss tala om dess lösningar.

Förhindra skräppostkommentarer

I stället för att direkt tillåta vem som helst att göra en POST-förfrågan på /wp-comments-post.php kan vi lägga till lite logik för att förhindra skräppostkommentarer till 100 %.

Steg 1. Begränsning av sökvägen för POST-kommentarer via frågeparametrar

Jag kommer att dela med mig av tre sätt, använd en metod.

Apache

  • Yoast > Gå till Verktyg > Filredigerare
  • RankMath > Gå till Allmänna inställningar > Redigera .htaccess
  • FTP/SSH > Kolla /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>

Om du använder LiteSpeed har den också stöd för .htaccess-filer. Du måste starta om efter genomförandet.

NGINX

location = /wp-comments-post.php {

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

Cloudflare

prevent spam comments
  • Logga in för att Cloudflare Dashboard
  • Gå till Firewall > Firewall Rules
  • Skapa en ny brandväggsregel med följande uttryck
FältOperatörVärde
URIcontainswp-comments-post.phpAnd
URL Query Stringdoes not equal45jpfAY9RcNeFP
  • Välj åtgärd: Blocka

I slutet kommer du att se ett uttryck

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

Steg 2. Korrigera URL:en för Comment POST vid Scroll-evenemanget

  • Lägg till nedanstående funktion med hjälp av insticksprogrammet Code Snippets eller temat functions.php
  • Se till att du använder rätt domän och formulär-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);

Om du använder GeneratePress Premium-tema kan du också lägga till ovanstående JS-del direkt med hjälp av Elements-modulen.

<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>
  • Lägg till en ny hook
  • Ny Hook Titel: Ändra URL för kommentarer i farten
  • Hook: WP Footer
  • Prioritet: 99
  • Plats: Inlägg – Alla inlägg
  • Publicera

Hur kan jag kontrollera om den fungerar?

  • Generellt sett returnerar WordPress 405 svar för GET-förfrågan på /wp-comments-post.php
  • Men efter ovanstående inställning bör du se Access Denied (Åtkomst nekad).
  • Och URL:en ska bara laddas om det finns en särskild Query String 45jpfAY9RcNeFP som vi lagt till för att förhindra skräppost. Du kan ändra den här frågeteckensträngen till något annat i alla ovanstående konfigurationer.
  • Källkoden visar vanligtvis sökvägen wp-comments-post.php, men om du inspekterar elementet efter rullning kommer du att märka att en frågeteckensträng har lagts till.

Resultat nr 1: Ingen skräppost

no spam

Resultat nr 2: Spammare blockeras

log

Bonustips för att göra standardkommentarsystemet ännu bättre

  • Som standard skickar WordPress kommentarsystem inget uppföljningsmeddelande till kommentarsförfattaren. För att åtgärda det här problemet kan du använda plugin Comment Reply Email Notification av Arno Welzel.

Om du gillar den här informationen kan du skicka den vidare till dina vänner. 🙏

Lämna en kommentar

20 svar på ”Nu kan du glömma skräppostkommentarer i WordPress 😎”

  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.

    Svara
  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?

    Svara
  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)?

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

    Svara
    • 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!

      Svara