Last active
July 7, 2022 17:13
-
-
Save Log1x/35f3f1f20e430d6a36a0c2e8e8f13ffc to your computer and use it in GitHub Desktop.
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
<?php | |
/** | |
* Validate HTML Forms. | |
* | |
* @param string $error | |
* @param \HTML_Forms\Form $form | |
* @param array $data | |
* @return string | |
*/ | |
add_filter('hf_validate_form', function ($error, $form, $data) { | |
if ($form->slug !== 'contact-us') { | |
return $error; | |
} | |
if (empty($data['first_name']) || empty($data['last_name'])) { | |
return 'required_field_missing'; | |
} | |
if (empty($data['email'] || ! is_email($data['email']))) { | |
return 'invalid_email'; | |
} | |
if (empty($data['terms'])) { | |
return 'invalid_terms'; | |
} | |
$recaptcha = new \ReCaptcha\ReCaptcha( | |
env('RECAPTCHA_PRIVATE_KEY') | |
); | |
$response = $recaptcha->setExpectedHostname( | |
wp_parse_url(home_url(), PHP_URL_HOST) | |
)->verify( | |
$data['g-recaptcha-response'] ?? '', | |
$_SERVER['REMOTE_ADDR'] | |
); | |
if (! $response->isSuccess()) { | |
return 'invalid_captcha'; | |
} | |
return $error; | |
}, 10, 3); | |
/** | |
* Customize the HTML Forms invalid captcha error. | |
* | |
* @return string | |
*/ | |
add_filter('hf_form_message_invalid_captcha', function () { | |
return __('Invalid recaptcha response.', 'html-forms'); | |
}); | |
/** | |
* Customize the HTML Forms invalid terms error. | |
* | |
* @return string | |
*/ | |
add_filter('hf_form_message_invalid_terms', function () { | |
return __('You must agree to the terms of service.', 'html-forms'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment