public
Created

Make changes to the comments form, even if it was loaded asynchronously.

  • Download Gist
anti-spam.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
/*
Anti-spam plugin
No spam in comments. No captcha.
wordpress.org/extend/plugins/anti-spam/
*/
 
(function($) {
function apply() {
$('.comment-form-anti-spam, .comment-form-anti-spam-2').hide(); // hide inputs from users
var answer = $('.comment-form-anti-spam input#anti-spam-a').val(); // get answer
$('.comment-form-anti-spam input#anti-spam-q').val(answer); // set answer into other input
 
if ($('#comments form input#anti-spam-q').length == 0) { // anti-spam input does not exist (could be because of cache or because theme does not use 'comment_form' action)
var current_date = new Date();
var current_year = current_date.getFullYear();
$('#comments form').append(
'<input type="hidden" name="anti-spam-q" id="anti-spam-q" value="'
+ current_year + '" />'
); // add whole input with answer via javascript to comment form
}
 
if ($('#respond form input#anti-spam-q').length == 0) { // similar, just in case (used because user could bot have #comments)
var current_date = new Date();
var current_year = current_date.getFullYear();
$('#respond form').append(
'<input type="hidden" name="anti-spam-q" id="anti-spam-q" value="'
+ current_year + '" />'
); // add whole input with answer via javascript to comment form
}
 
if ($('form#commentform input#anti-spam-q').length == 0) { // similar, just in case (used because user could bot have #respond)
var current_date = new Date();
var current_year = current_date.getFullYear();
$('form#commentform').append(
'<input type="hidden" name="anti-spam-q" id="anti-spam-q" value="'
+ current_year + '" />'
); // add whole input with answer via javascript to comment form
}
}
 
$(document).on('ready', function() {
apply();
});
 
$(document).ajaxSuccess(function() {
apply();
});
})(jQuery);

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.