Spam Comments Prevention Without any Plugin In GeneratePress

We all know the frustration of spam comments. They’re a pain to clean up and sometimes they get through to your blog post content. In this article, I will show you some tips on how to block spam comments without any plugin.

GeneratePress is an excellent WordPress theme for bloggers, but it doesn’t have any built-in protection against spam comments. However, there are some free plugins you can use that will help prevent them from showing up on your site in the first place! So if you find yourself dealing with it, then check out this article for some tips!

Spam Comments Prevention Without any Plugin In GeneratePress

How to Stop WordPress Comments Spam?

Comments spam is a problem that all WordPress site owners face, but there are some simple steps you can take to successfully stop your site from being targeted.

If you aren’t familiar with comments spam it’s basically when spammers exploit the commenting system on popular sites by leaving links in their comments.

There are two basic ways we can get rid of spam. The first one is to remove the website field from the comment box and the second one is adding a suffix to comment submit action i.e. wp-comments-post.php?e2a521bc01c1ca09e173bcf65bcc97e9.

Let’s check each code and also check how to apply it to the GeneratePress theme.

How to remove the website (URL) field from the comment box?

Usually, spammers target the URL field in the comments section. And when we click on the name of that spammer, the URL is redirected to the URL provided in the website field. In this example, we are going to use the Code Snippets plugin. Let’s check step by step.

  • Go to Snippets->Add New.
  • Enter some title.
  • Paste the below code.
  • Select Run snippet everywhere option.
  • Click on Save Changes.
  • Finally Click on activate to activate the hook.
add_action( 'after_setup_theme', 'chb_add_comment_filter' );
function chb_add_comment_filter() {
    add_filter( 'comment_form_default_fields', 'chb_remove_comment_url', 20 );
}
function chb_remove_comment_url($fields) {
    unset($fields['url']);
    return $fields;
}

Adding a suffix to comment submit action

If you notice the comment form action URL where the form is submitted, you will see an URL like this http://example.com/wp-comments-post.php.

Usually, spammers search wp-comments-post.php and post spam comments through bots. But if we add a random query string to the wp-comments-post.php URL, the bot can’t find it.

To add random query string after wp-comments-post.php, you have to use a filter comment_form_defaults. Add the code below using code snippets.

function codehasbug_stop_spam( $defaults ) {
  $defaults['action'] = site_url('/wp-comments-post.php?'.bin2hex(random_bytes(20)));
  return $defaults;
}
add_filter( 'comment_form_defaults', 'codehasbug_stop_spam' );

Final Word

GeneratePress doesn’t have any built-in spam prevention system. So we have to depend on third-party plugins. With the above method, you can have all the benefits of an extra layer of protection without having to worry about compatibility issues or breaking changes in future updates. Check out our other WordPress for more information, and let us know if you want help adding this feature to your site today!

About Ashis Biswas

A web developer who has a love for creativity and enjoys experimenting with the various techniques in both web designing and web development. If you would like to be kept up to date with his post, you can follow him.

Leave a Comment