How to add Google Captcha (reCAPTCHA) plugin to a custom form on my WordPress website?

Follow the instructions below in order to add Google Captcha (reCAPTCHA) plugin to your custom PHP or HTML form:
1. Install the Google Captcha (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 -> Google Captcha); 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.

If you have followed all steps, but the problem remains, we can help you to configure your Google Captcha custom form. This will be a paid service since there are a lot of different custom forms and the code should be inserted individually into each of them, so we need some time to study each unique case.

Have more questions? Submit a request