XTRA 64

If you are website owner, you probably have encountered spam comments, They are really annoying and surely you have looking for a way to get rid of spam comments.

If you search about preventing spam comments in search engines like Google, surely you will find some free and paid plugins which some of them works and some of them not. But usually website owners don’t like to use plugin for this type of problems due to performance of their website and also security risk.

So what’s the easy solution for preventing spam comments? We have a quick solution here, by adding small PHP codes to your theme or child theme functions.php file you will prevent all spam comments on your website.

Replace website field in comments form with security field

Just copy and paste following code into your theme or child theme functions.php file and save it.

// Add security field to comments.
function xtra_add_comment_field( $fields ) {

	if ( isset( $fields['url'] ) ) {

		$num_a = rand( 1, 10 );
		$num_b = rand( 1, 10 );

		$fields['url'] = '<p class="comment-form-url"><label for="url">' . $num_a . ' + ' . $num_b . ' = ?</label><input id="url" name="url" type="text" value="" size="30" maxlength="200" /><input name="security" type="hidden" value="' . md5( $num_a + $num_b ) . '" /></p>';

	}

	return $fields;
}
add_filter( 'comment_form_default_fields', 'xtra_add_comment_field' );

// Check security field for comments.
function xtra_verify_comment_field( $commentdata ) {

	if ( ! empty( $_POST['url'] ) && ! empty( $_POST['security'] ) ) {

		if ( md5( $_POST['url'] ) !== $_POST['security'] ) {
			wp_die( 'Error: Security answer is wrong' );
		}

		$commentdata['comment_author_url'] = '';

	} else {
		wp_die( 'Error: Security answer is wrong' );
	}

	return $commentdata;
}
add_filter( 'preprocess_comment', 'xtra_verify_comment_field' );

Save file and refresh your post page then check your comments form. Or if you want to add security question field under other fields, try bellow function codes.

Add security field in comments form under website field

But If you want to keep your comments form website field and want to add one extra security field bellow it, you can use following codes:

// Add security field to comments.
function xtra_add_comment_fields( $fields ) {

	if ( isset( $fields['question'] ) ) {

		$num_a = rand( 1, 10 );
		$num_b = rand( 1, 10 );

		$fields['question'] = '<p class="comment-form-question"><label for="question">' . $num_a . ' + ' . $num_b . ' = ?</label><input id="question" name="question" type="text" value="" size="30" maxlength="200" /><input name="security" type="hidden" value="' . md5( $num_a + $num_b ) . '" /></p>';

	}

	return $fields;
}
add_filter( 'comment_form_default_fields', 'xtra_add_comment_fields' );

// Check security field for comments.
function xtra_verify_comment_field( $commentdata ) {

	if ( ! empty( $_POST['question'] ) && ! empty( $_POST['security'] ) ) {

		if ( md5( $_POST['question'] ) !== $_POST['security'] ) {
			wp_die( 'Error: Security answer is wrong' );
		}
		$commentdata['comment_author_url'] = '';
		
	} else {
		wp_die( 'Error: Security answer is wrong' );
	}

	return $commentdata;
}
add_filter( 'preprocess_comment', 'xtra_verify_comment_field' );

No comment

Leave a Reply

Your email address will not be published. Required fields are marked *