· 9 years ago · Nov 14, 2016, 11:16 AM
1/*
2 This snippet will allow the use of ReCaptcha 2.0 with Event Manager for Wordpress
3 Replace your-site-key and your-secrect-key
4 Also the Error handling is in french right now. You can edit the text to your liking.
5 Credits : Google, codeforgeek.com & myself (gil@cynaptek.com)
6 version : 0.2
7*/
8
9function my_em_event_form_captcha(){
10 // Use Captcha only if the user is not logged in
11 if ( !is_user_logged_in() ){
12 ?><p><script src="https://www.google.com/recaptcha/api.js" async defer></script><div class="g-recaptcha" data-sitekey="your-site-key"></div></p><?php
13 }
14}
15add_action('em_booking_form_footer','my_em_event_form_captcha');
16
17
18function my_em_event_form_captcha_check($this) {
19 global $EM_Booking;
20
21 if ( !is_user_logged_in() ){
22 $captcha;
23
24 if(isset($_POST['g-recaptcha-response'])){
25 $captcha=$_POST['g-recaptcha-response'];
26 }
27
28 if(!$captcha){
29 $EM_Booking->errors[]=__('Veuillez valider le captcha');
30 }
31 $secretKey = "your-secrect-key";
32 $ip = $_SERVER['REMOTE_ADDR'];
33 $response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secretKey."&response=".$captcha."&remoteip=".$ip);
34 $responseKeys = json_decode($response,true);
35 if(intval($responseKeys["success"]) !== 1) {
36 $EM_Booking->errors[]=__('Mauvais captcha');
37 unset($_REQUEST['em_tickets']);
38 }
39 }
40}
41add_filter('em_booking_get_post','my_em_event_form_captcha_check',10,$this);