Skip to content

Commit 9363a0d

Browse files
author
Antti Kuosmanen
committed
Add ReCaptcha example to readme
- Also fix some typos in readme
1 parent 6fdc1b7 commit 9363a0d

1 file changed

Lines changed: 47 additions & 2 deletions

File tree

readme.md

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,9 @@ wp_enqueue_script('wplf-form-js');
8989
wp_localize_script( 'wplf-form-js', 'ajax_object', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );
9090
```
9191

92-
### Add own css classes to form output
92+
### Add your own css classes to the form element
9393

94-
You can use the attribute xclass inside the shortcode to set own extra css classes.
94+
You can use the xclass attribute inside the shortcode to add your own extra classes.
9595

9696
```
9797
[libre-form id="1" xclass="extra"]
@@ -123,3 +123,48 @@ function my_form_validation( $return ) {
123123
return $return;
124124
}
125125
```
126+
127+
### wplf_validate_submission example: Google ReCaptcha integration
128+
129+
```php
130+
/**
131+
* ReCaptcha for WP Libre Form
132+
*/
133+
add_filter( 'wplf_validate_submission', 'wplf_recaptcha' );
134+
function wplf_recaptcha( $return ) {
135+
// skip this validation if submission has already failed
136+
if( ! $return->ok ) {
137+
return $return;
138+
}
139+
140+
$form = get_post( (int) $_POST['_form_id'] );
141+
if( false === strpos( $form->post_content, 'g-recaptcha' ) ) {
142+
// this form doesn't have recaptcha
143+
return $return;
144+
}
145+
146+
$secret = 'XXXX'; // substitute with your own secret recaptcha key
147+
148+
$options = [
149+
'http' => [
150+
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
151+
'method' => 'POST',
152+
'content' => http_build_query([
153+
'secret' => $secret,
154+
'response' => $_POST['g-recaptcha-response'],
155+
])
156+
],
157+
];
158+
$context = stream_context_create( $options );
159+
$result = file_get_contents( 'https://www.google.com/recaptcha/api/siteverify', false, $context );
160+
161+
$captcha_obj = json_decode( $result );
162+
163+
if( false === $captcha_obj->success ) {
164+
$return->ok = 0;
165+
$return->error = sprintf( __("Please prove you're not a robot before submitting."), intval( $_POST['_form_id'] ) );
166+
}
167+
168+
return $return;
169+
}
170+
```

0 commit comments

Comments
 (0)