Add Captcha plugin to a custom form on my Wordpress website

Follow the instructions below in order to add Captcha plugin to your custom PHP or HTML form:

1. Install Captcha plugin and activate it.

2. (Optional) If you would like to use your own settings for the custom forms you have (for example, for your contact and sign up forms), please follow the steps below:
- Go to the plugin settings page;
- Open Custom code tab;
- Mark "Activate" checkbox in the "Editing bws-custom-code.php" section;
- Add the following code:

`function add_my_forms( $forms ) {
   $forms['my_contact_form'] = "Form Display Name";
   return $forms;
add_filter( 'cptch_add_form', 'add_my_forms' );`

Please don't use the next form slugs since they are predefined by plugin settings: general, wp_login, wp_register, wp_lost_password, wp_comments, bws_contact, bws_subscriber, buddypress_register, buddypress_comments, buddypress_group, cf7_contact, woocommerce_login, woocommerce_register, woocommerce_lost_password, woocommerce_checkout.
- Save file changes;
- Go to the "Settings" tab on the plugin settings page (Admin Dashboard -> Captcha); If everything is OK, you will see your form in 'Enable Captcha for' => 'External plugins' ( with labels which you specified in the "cptch_add_form_tab" hook call function ).
- Enable it and configure form options as you need;
- Click "Save changes";
If you don`t want to use your own settings for CAPTCHA displaying in your custom form, it will use the settings from "General" block on the plugin settings.

3. Open the file with the form (where you would like to add CAPTCHA);

4. Find a place to insert the code for the CAPTCHA output;

5. If you completed the instructions in point 2, then you should add:

`<?php echo apply_filters( 'cptch_display', '', 'my_contact_form' ); ?>`

In this example, the second parameter is a slug for your custom form.

Otherwise, insert the following lines:

`<?php echo apply_filters( 'cptch_display', '' ); ?>`

6. After that, you should add the following lines to the function of the entered data checking:

`<?php $error = apply_filters( 'cptch_verify', true );
if ( true === $error ) { /* the CAPTCHA answer is right */
   /* do necessary action */
} else { /* the CAPTCHA answer is wrong or there are some other errors */
   echo $error; /* display the error message or do other necessary actions in case when the CAPTCHA test was failed */
} ?>`

If there is a variable in the check function responsible for the errors output, you can concatenate variable $error to this variable. If the 'cptch_verify' filter hook returns 'true', it means that you have entered the CAPTCHA answer properly. In all other cases, the function will return the string with the error message.


Have more questions? Submit a request