Description
The WP_Members_Captcha object class has a number of functions that you may call statically in order to implement custom instances of the captchas supported by WP-Members. This includes the following captcha types:
- Google reCAPTCHA v2 (API key required)
- Google reCAPTCHA v3 (API key required)
- Really Simple CAPTCHA (Really Simple CAPTCHA plugin required)
- hCaptcha (API key required)
Methods
There are two methods required to implement one of the captchas – one to display the captcha, the other to validate that it passed.
show()
The show() method is used to display the captcha using the following parameters:
$type The type of captcha being displayed. Required (recaptcha_v2|recaptcha_v3|rs_captcha|hcaptcha)
$key The “public” key for the captcha being displayed. For reCAPTCHA, this is your public API key, for hCatpcha it is your API key. This parameter is omitted for Really Simple CAPTCHA.
Return value is a string of HTML for the captcha being displayed.
validate()
The validate() method is used to validate form submission and make sure the captcha was passed.
$type The type of captcha being used. Required (recaptcha_v2|recaptcha_v3|rs_captcha|hcaptcha)
$key The “private/secret” key for the captcha being displayed. For reCAPTCHA, this is your private API key, for hCatpcha it is your secret key. This parameter is omitted for Really Simple CAPTCHA.
Return value is a true|false boolean
Usage
/** * To display a captcha, use the show() method passing both the * captcha type (see docs) and your public api key. The method * returns a string that you can either append to your form * string or echo/print, depending on what hook you are using * to display it. */ $html = WP_Members_Captcha::show( $type, $public_key ); /** * To validate a captcha upon form submission, use the validate() * method passing both the captcha type (see docs) and your * private/secret api key. This returns a true|false boolean. */ $passed = WP_Members_Captcha::validate( $type, $private_key );
Notes
- Really Simple CAPTCHA requires that you have the Really Simple CAPTCHA plugin installed and activated.
- Really Simple CAPTCHA does not use an API key for display or validation, so omit the second argument of the method.
Changelog
- Introduced in WP-Members 3.3.5
Source
The WP_Members_Captcha static methods are in /includes/class-wp-members-captcha.php