<?php
/**
* Handle form submission.
*/
add_action( 'admin_post_nopriv_contact_form_submission', 'handle_contact_form_submission' );
add_action( 'admin_post_contact_form_submission', 'handle_contact_form_submission' );
function handle_contact_form_submission() {
// Check if the nonce is set.
if ( isset( $_POST['contact_form_nonce'] ) && wp_verify_nonce( $_POST['contact_form_nonce'], 'contact_form_action' ) ) {
// Check if the name is set.
if ( isset( $_POST['name'] ) ) {
$name = sanitize_text_field( $_POST['name'] );
}
// Check if the email is set.
if ( isset( $_POST['email'] ) ) {
$email = sanitize_email( $_POST['email'] );
}
// Check if the name and email is set.
if ( isset( $name ) && isset( $email ) ) {
// Validate Google reCAPTCHA
$recaptcha_response = $_POST['g-recaptcha-response'];
$recaptcha_secret = 'YOUR_RECAPTCHA_SECRET_KEY';
$recaptcha_verify = wp_safe_remote_get("https://www.google.com/recaptcha/api/siteverify?secret=6LezqOMnAAAAAFLcXYKpO0KKnhhvybmh1l-ZmGez&response=$recaptcha_response");
$recaptcha_data = json_decode($recaptcha_verify['body']);
if ($recaptcha_data->success) {
// CAPTCHA validation passed
// Handle file upload if needed
// Send email or save data to database
wp_safe_redirect($_SERVER['HTTP_REFERER']);
} else {
// CAPTCHA validation failed
echo "reCAPTCHA verification failed. Please try again.";
}
}
else{
wp_safe_redirect($_SERVER['HTTP_REFERER']);
echo "Please fill all the required fields.";
}
}
}