Created
May 20, 2022 20:12
-
-
Save stefanmm/691d06542aa4034632cbe813cdb82273 to your computer and use it in GitHub Desktop.
Simple honeypot for WP comments
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// We will add a fake input field to the WP comment form and make it invisible for humans | |
// The "firstname" field name is not used by WP (by default) so we can use it | |
function simple_honey_pot_44587513(){ | |
ob_start(); | |
?> | |
<style> | |
.rpodum { | |
top: 0; | |
left: 0; | |
width: 0; | |
height: 0; | |
opacity: 0; | |
z-index: -1; | |
position: absolute; | |
} | |
</style> | |
<input name="firstname" type="text" id="firstname" class="rpodum" autocomplete="off" tabindex="-1" /> | |
<?php | |
echo ob_get_clean(); | |
} | |
add_action('comment_form', 'simple_honey_pot_44587513'); | |
// Since our fake field is hidden for human eyes, it will not be populated by human | |
// Robots will fill all fields inside the form - even invisible ones! | |
function check_simple_honey_pot_44587513($approved) { | |
// If our invisible field IS empty, then everything is good | |
// If it is NOT empty, mark the comment as "spam" | |
return (empty($_POST['firstname'])) ? $approved : 'spam'; | |
} | |
add_filter('pre_comment_approved', 'check_simple_honey_pot_44587513', 9999, 1); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment