Simple Google reCAPTCHA plugin for WordPress
This is a very simple integration plugin for WordPress, to make it easy for developers to add reCAPTCHA to custom forms.
With this plugin you will have to manually add the widget where you want it to be shown and validate the response on the appropiate place.
So, as you may have guessed, this is not an automagic plugin that adds reCAPTCHA into all of WP forms (comments, login, etc). This is a plugin for developers who want to easily output a reCAPTCHA widget and then validate its response, knowing what they are doing.
- WordPress 5.x
- PHP 5.6+
Clone/download the repo and install the plugin by unziping the contents of the zip into the wp-content/plugins
folder of your WP installation, rename the resulting folder to wp-recaptcha
and activate it through the WordPress admin dashboard.
Once installed you will see a 'reCAPTCHA' entry on the left-side menu, click it and you will see the options page. There you will require to set the site
and secret
keys.
If you don't have a site
key, click here to get one.
Then you must show the widget using the widget
function directly on the template:
if ( class_exists('ReCaptcha') ) {
ReCaptcha::widget();
}
Tip: It is recommended that you check for the class before trying to use it, just in case the plugin had been disabled.
Once the widget is being shown, check the response on you form processing logic using the validate
function:
if ( class_exists('ReCaptcha') ) {
$recaptcha = get_item($_POST, 'g-recaptcha-response');
$valid = ReCaptcha::validate($recaptcha);
if (! $valid ) {
// Not valid, abort processing
echo 'You ARE a robot';
die();
}
}
// Otherwise continue as normal
As simple as that.
You may customize the default widget by passing some parameters to the widget
function:
theme
- The theme to use, eitherlight
ordark
(default:light
)size
- The size of the widget, eithercompact
ornormal
(default:normal
)tabindex
- The tab index property (default:0
)callback
- Callback for successful responeexpired-callback
- Callback for expired widgeterror-callback
- Callback for error
For example:
if ( class_exists('ReCaptcha') ) {
$params = [];
$params['theme'] = 'dark';
ReCaptcha::widget($params);
}
You can find more info here: https://developers.google.com/recaptcha/docs/display#render_param
The plugin will enqueue a recaptcha
script with the path to the reCAPTCHA library. This may conflict with other reCAPTCHA plugins on the site.
Also, your server might block connections to the verification URL (https://www.google.com/recaptcha/api/siteverify) so please make sure you can reach it before submitting an issue.
Finally, WordPress might make all this fail without a clear cause, so if you are submitting an issue please provide as much info as possible about the site in question (server type, WP version, PHP version, etc).
MIT licensed
Author: biohzrdmx <github.com/biohzrdmx>