Created
October 20, 2015 19:34
-
-
Save Vamoss/5a92dee937692b9727df to your computer and use it in GitHub Desktop.
Recaptcha for BuddyPress (Stop spammers)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
RECAPTCHA | |
Stop wordpress with buddypress spam | |
Remember to replace "YOUR SECRET" and "YOUR SITE KEY". | |
**/ | |
//these files are required, you can find it here: | |
//https://github.com/google/recaptcha | |
require_once __DIR__ . '/ReCaptcha/RequestMethod.php'; | |
require_once __DIR__ . '/ReCaptcha/RequestMethod/Post.php'; | |
require_once __DIR__ . '/ReCaptcha/Response.php'; | |
require_once __DIR__ . '/ReCaptcha/RequestParameters.php'; | |
require_once __DIR__ . '/ReCaptcha/ReCaptcha.php'; | |
function bph_validate_captcha(){ | |
// Register API keys at https://www.google.com/recaptcha/admin | |
$secret = 'YOUR SECRET'; | |
global $bp; | |
if (isset($_POST['g-recaptcha-response'])): | |
// If the form submission includes the "g-captcha-response" field | |
// Create an instance of the service using your secret | |
$recaptcha = new \ReCaptcha\ReCaptcha($secret); | |
// Make the call to verify the response and also pass the user's IP address | |
$resp = $recaptcha->verify($_POST['g-recaptcha-response'], $_SERVER['REMOTE_ADDR']); | |
if ($resp->isSuccess()): | |
// If the response is a success, that's it! | |
//do nothing | |
else: | |
// If it's not successful, then one or more error codes will be returned. | |
$bp->signup->errors['bph_field'] = __('This is a required field','buddypress'); | |
endif; | |
else: | |
$bp->signup->errors['bph_field'] = __('This is a required field','buddypress'); | |
endif; | |
return; | |
} | |
function bph_show_captcha(){ | |
?> | |
<div style="clear:both; float: right;" class="bph_field_container"> | |
<div class="g-recaptcha" data-sitekey="YOUR SITE KEY"></div> | |
<script type="text/javascript" | |
src="https://www.google.com/recaptcha/api.js?hl=pt-BR"> | |
</script> | |
<?php do_action( 'bp_bph_field_errors' ) ?> | |
</div> | |
<?php | |
} | |
add_action('bp_signup_validate', 'bph_validate_captcha'); | |
add_action('bp_before_registration_submit_buttons', 'bph_show_captcha'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment