Skip to content

Instantly share code, notes, and snippets.

@michaelbonner
Last active August 29, 2015 14:10
Show Gist options
  • Save michaelbonner/867ae7ddced3389dfe87 to your computer and use it in GitHub Desktop.
Save michaelbonner/867ae7ddced3389dfe87 to your computer and use it in GitHub Desktop.
Stop WordPress Comment Spam
// check to see if the hidden input exists in submission
function preprocess_new_comment($commentdata) {
if(!isset($_POST['is_legit'])) {
die();
}
return $commentdata;
}
add_action('preprocess_comment', 'preprocess_new_comment');
// add hidden field to form
function add_field_to_comment_form() {
?>
<script>
jQuery( document ).ready( function($) {
$('#commentform').prepend('<input type="hidden" name="is_legit" value="1" />');
});
</script>
<?
}
add_action( 'wp_footer', 'add_field_to_comment_form' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment