Hướng dẫn code function kiểm tra chặn spam Contact Form 7 (CF7) dựa trên số lượng cuộn màn hình
Dán vào function.php
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 |
/* * 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 }); |
Quý khách cần tư vấn thiết kế web giá rẻ, vui lòng nhấn nút bên dưới:
Chào ! Bạn thấy nội dung này thế nào?
Xem thêm: