Dán vào function.php
/*
* Check spam cf7 bằng scroll
* Author: levantoan.com
* */
add_filter('wpcf7_form_elements', 'devvn_check_scroll_form_cf7');
function devvn_check_scroll_form_cf7($html){
$html = '<div style="display: none"><p><span class="wpcf7-form-control-wrap" data-name="devvn-scroll"><input size="40" class="wpcf7-form-control wpcf7-text" aria-invalid="false" value="0" type="text" name="devvn-scroll"></span></p></div>' . $html;
return $html;
}
add_action('wpcf7_posted_data', 'devvn_check_scroll_form_cf7_vaild');
function devvn_check_scroll_form_cf7_vaild($posted_data) {
$submission = WPCF7_Submission::get_instance();
$scroll = isset($posted_data['devvn-scroll']) ? intval($posted_data['devvn-scroll']) : 0;
//nếu form ở ngay trên đầu page thì thay 5000 thành số nhỏ hơn. ví dụ 200
if (!$scroll || $scroll <= 5000) {
$submission->set_status( 'spam' );
$submission->set_response( 'You are spamer' );
}
unset($posted_data['devvn-scroll']);
return $posted_data;
}
add_action('wp_footer', function (){
?>
<script>
const scrollInputs = document.querySelectorAll('input[name="devvn-scroll"]');
if(scrollInputs.length > 0) {
let accumulatedScroll = 0;
function devvnCheckScroll() {
accumulatedScroll += window.scrollY;
scrollInputs.forEach(input => {
input.value = accumulatedScroll;
});
//nếu form ở ngay trên đầu page thì thay 6000 thành số nhỏ hơn. ví dụ 300
if (accumulatedScroll >= 6000) {
window.removeEventListener('scroll', devvnCheckScroll);
}
}
window.addEventListener('scroll', devvnCheckScroll);
}
</script>
<?php
});
Chào ! Bạn thấy nội dung này thế nào?





