// Handle contact form submission
function handle_contact_form() {
if (!isset($_POST[‘contact_nonce’]) || !wp_verify_nonce($_POST[‘contact_nonce’], ‘contact_form_nonce’)) {
wp_redirect(home_url(‘/contact-us/?error=mail’));
exit;
}
$name = sanitize_text_field($_POST[‘name’]);
$email = sanitize_email($_POST[’email’]);
$phone = sanitize_text_field($_POST[‘phone’]);
$message = sanitize_textarea_field($_POST[‘message’]);
if (empty($name) || empty($email) || empty($message)) {
wp_redirect(home_url(‘/contact-us/?error=empty’));
exit;
}
if (!is_email($email)) {
wp_redirect(home_url(‘/contact-us/?error=email’));
exit;
}
$to = get_option(‘admin_email’); // or info@yehaniagaratours.com
$subject = “New Contact Inquiry – $name”;
$body = “Name: $name\nEmail: $email\nPhone: $phone\n\nMessage:\n$message”;
$headers = array(‘Reply-To: ‘ . $email);
$mail_sent = wp_mail($to, $subject, $body, $headers);
if ($mail_sent) {
wp_redirect(home_url(‘/contact-us/?success=1’));
} else {
wp_redirect(home_url(‘/contact-us/?error=mail’));
}
exit;
}
add_action(‘admin_post_nopriv_contact_form’, ‘handle_contact_form’);
add_action(‘admin_post_contact_form’, ‘handle_contact_form’);