Skip to content

Instantly share code, notes, and snippets.

@neilgee
Last active March 31, 2025 21:47
Show Gist options
  • Save neilgee/5681ab97ad2d9883ddfa28a4690d1953 to your computer and use it in GitHub Desktop.
Save neilgee/5681ab97ad2d9883ddfa28a4690d1953 to your computer and use it in GitHub Desktop.
Cloudflare Turnstilefor WordPress Comments form
<?php <~~ don't add me in
/*
* Adding Cloudflare Turnstile to WordPress Comment
* https://redpishi.com/wordpress-tutorials/cloudflare-turnstile-captcha-wordpress/
*/
function cloudflare_key(){
$sitekey= "00000000000000000000000";
$secretkey= "000000000000000000000";
return [$sitekey,$secretkey];
}
add_action("wp_head", function(){
wp_enqueue_script('cloudflare-turnstile', 'https://challenges.cloudflare.com/turnstile/v0/api.js');
} );
/*
* Adding Cloudflare Turnstile to WordPress Comment
* @link https://redpishi.com/wordpress-tutorials/cloudflare-turnstile-captcha-wordpress/
*/
function is_valid_captcha($captcha) {
if (!$captcha) {
return false;
}
$secretKey = cloudflare_key()[1];
$ip = $_SERVER['REMOTE_ADDR'];
$url_path = 'https://challenges.cloudflare.com/turnstile/v0/siteverify';
$data = array('secret' => $secretKey, 'response' => $captcha, 'remoteip' => $ip);
$options = array(
'http' => array(
'method' => 'POST',
'content' => http_build_query($data))
);
$stream = stream_context_create($options);
$result = file_get_contents(
$url_path, false, $stream);
$response = $result;
$responseKeys = json_decode($response,true);
if(intval($responseKeys["success"]) !== 1) {
return false;
} else {
return true;
}
}
add_action('init', function(){
if (!is_user_logged_in() ) {
add_action('pre_comment_on_post', function(){
$recaptcha = $_POST['cf-turnstile-response'];
if (empty($recaptcha))
wp_die( __("<b>ERROR:</b> please select <b>I'm not a robot!</b><p><a href='javascript:history.back()'>« Back</a></p>"));
else if (!is_valid_captcha($recaptcha))
wp_die( __("<b>please select I'm not a robot!</b>"));
} );
add_filter('comment_form_defaults',function ($submit_field) {
$submit_field['submit_field'] = '<div class="cf-turnstile" data-sitekey="'.cloudflare_key()[0].'"></div><br>'.$submit_field['submit_field'];
return $submit_field;
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment