How to add reCaptcha plugin to a custom form on my WordPress website?
Author
author
Date
2020-08-13 23:53
Views
2632
Follow the instructions below in order to add reCAPTCHA plugin to your custom PHP or HTML form:
1. Install the reCAPTCHA plugin and activate it.
2. (Optional) If you would like to have an ability to enable and disable the reCAPTCHA for your custom form on the plugin settings page, please add the following code to the ‘functions.php’ file of your theme:
function add_custom_recaptcha_forms( $forms ) {
$forms['my_custom_form'] = array( "form_name" => "Custom Form Name" );
return $forms;
}
add_filter( 'gglcptch_add_custom_form', 'add_custom_recaptcha_forms' );
In this example, 'my_custom_form' is a slug of your custom form.
Please don't use the following form slugs since they are predefined by plugin settings: login_form, registration_form, reset_pwd_form, comments_form, contact_form, cf7, si_contact_form, jetpack_contact_form, sbscrbr, bbpress_new_topic_form, bbpress_reply_form, buddypress_register, buddypress_comments, buddypress_group, woocommerce_login, woocommerce_register, woocommerce_lost_password, woocommerce_checkout, wpforo_login_form, wpforo_register_form, wpforo_new_topic_form, wpforo_reply_form.
- Save file changes;
- Go to the "Settings" tab on the plugin settings page (Admin Dashboard -> reCaptcha); If everything is OK, you will see your form in 'Enable reCAPTCHA for' => 'Custom Forms' (with labels which you specified in the "gglcptch_add_custom_form" hook call function).
- Enable it and configure form options as you need;
- Click "Save Changes" button;
If you don't add this code, no option for your custom form will be displayed on the plugin settings page and the reCAPTCHA will be always displayed in your custom form.
3. Open the file with the form (where you would like to add reCAPTCHA);
4. Find a place to insert the code for the reCAPTCHA output;
If you completed the instructions in p. 2, then you should add:
<?php echo apply_filters( 'gglcptch_display_recaptcha', '', 'my_custom_form' ); ?>
In this example, the second parameter is a slug of your custom form.
Otherwise, insert the following line:
<?php echo apply_filters( 'gglcptch_display_recaptcha', '' ); ?>
5. After that, you should add the following lines to the function of the entered data checking.
If you completed the instructions in p. 2, then you should add:
<?php $check_result = apply_filters( 'gglcptch_verify_recaptcha', true, 'string', 'my_custom_form' );
if ( true === $check_result ) { /* the reCAPTCHA answer is right */
/* do necessary action */
} else { /* the reCAPTCHA answer is wrong or there are some other errors */
echo $check_result; /* display the error message or do other necessary actions in case when the reCAPTCHA test was failed */
} ?>
In this example, the third parameter is a slug of your custom form.
Otherwise, insert the following lines:
<?php $check_result = apply_filters( 'gglcptch_verify_recaptcha', true, 'string' );
if ( true === $check_result ) { /* the reCAPTCHA answer is right */
/* do necessary action */
} else { /* the reCAPTCHA answer is wrong or there are some other errors */
echo $check_result; /* display the error message or do other necessary actions in case when the reCAPTCHA test was failed */
} ?>
If there is a variable in the check function responsible for the errors output, you can concatenate variable $check_result to this variable. If the 'gglcptch_verify_recaptcha' filter hook returns 'true', it means that you have entered the reCAPTCHA answer properly. In all other cases, the function will return the string with the error message.
Total Reply 0
Total 104
Number | Title | Author | Date | Votes | Views |
103 |
Join a Microsoft Teams meeting
author
|
2020.09.22
|
Votes 0
|
Views 3419
|
author | 2020.09.22 | 0 | 3419 |
102 |
How to delete songs and albums from your Google Play Music library
author
|
2020.09.19
|
Votes 0
|
Views 3585
|
author | 2020.09.19 | 0 | 3585 |
101 |
Watch Prime Video on Chromecast
author
|
2020.09.14
|
Votes 0
|
Views 3617
|
author | 2020.09.14 | 0 | 3617 |
100 |
How to Fix the Windows 10 We Couldn’t Connect to the Update Service Issue (2)
author
|
2020.09.09
|
Votes 0
|
Views 4383
|
author | 2020.09.09 | 0 | 4383 |
99 |
IONOS Joomla! Standard Installation
author
|
2020.09.02
|
Votes 0
|
Views 2283
|
author | 2020.09.02 | 0 | 2283 |
98 |
How to improve the life of a Battery: (1)
author
|
2020.08.25
|
Votes 0
|
Views 2318
|
author | 2020.08.25 | 0 | 2318 |
97 |
Laptop battery not charging
author
|
2020.08.21
|
Votes 0
|
Views 2624
|
author | 2020.08.21 | 0 | 2624 |
96 |
Plugged in, not charging (1)
author
|
2020.08.21
|
Votes 0
|
Views 2429
|
author | 2020.08.21 | 0 | 2429 |
95 |
How to add reCaptcha plugin to a custom form on my WordPress website?
author
|
2020.08.13
|
Votes 0
|
Views 2632
|
author | 2020.08.13 | 0 | 2632 |
94 |
I can't log in to the WordPress admin dashboard after installing and activating Limit Attempts plugin
author
|
2020.08.13
|
Votes 0
|
Views 2299
|
author | 2020.08.13 | 0 | 2299 |