First connect the CAPTCHA script (add to: functions.php)
function onwp_enqueue_frontend() {
wp_enqueue_script('ha-recaptcha', 'https://www.google.com/recaptcha/api.js', array('jquery'), '1.0', true);
}
add_action('wp_enqueue_scripts', 'onwp_enqueue_frontend');
In the form of adding the following html code to display the captcha
<div class="g-recaptcha" data-sitekey="YOU_SITE_CODE"></div>
YOU_SITE_CODE – replaced by your code
Next, add the CAPTCHA validation feature
$recaptcha = $_POST['data']['g_recaptcha_response'];
if (!empty($recaptcha)) {
$google_url = "https://www.google.com/recaptcha/api/siteverify";
$secret="YOU_SECRET_CODE";
$ip = $_SERVER['REMOTE_ADDR'];
$url = $google_url . "?secret=" . $secret . "&response=" . $recaptcha . "&remoteip=" . $ip;
$res = onwp_getCurlData($url);
$res = json_decode($res, true);
//reCaptcha введена
if (empty($res['success'])) {
// Error, please enter the captcha again
} else {
// all passed successfully perform your actions
}
} else {
// Error, please enter the captcha again
}
YOU_SECRET_CODE – replaced by your code
Function executes the query(add to functions.php):
function onwp_getCurlData($url) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_TIMEOUT, 10);
curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.16) Gecko/20110319 Firefox/3.6.16");
$curlData = curl_exec($curl);
curl_close($curl);
return $curlData;
}