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 your theme folder, for example, 'your-wp-site/wp-content/themes/your-theme-name' where your-theme-name is theme name you currently using;
- Open functions.php file;
- Add the following code to the end of the file: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 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.
If you completed the instructions in point 2, then you should add:
<?php $error = apply_filters( 'cptch_verify', true, 'string', 'my_contact_form' );
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 */
} ?>
In this example, the third parameter is a slug for your custom form.
Otherwise, insert the following lines:<?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 Captcha answer properly. In all other cases, the function will return the string with the error message.
This article related to Captcha by BestWebSoft plugin
Additional resources: Captcha - User Guide
Still need help? Feel free to contact our friendly support team. Create a private ticket