// Handle LiiFoo Contact Form submission
add_action(‘template_redirect’, function() {
$post_data = filter_input_array(INPUT_POST, [
‘liifoo_contact_action’ => FILTER_SANITIZE_SPECIAL_CHARS,
‘your-name’ => FILTER_SANITIZE_SPECIAL_CHARS,
‘your-email’ => FILTER_SANITIZE_EMAIL,
‘your-subject’ => FILTER_SANITIZE_SPECIAL_CHARS,
‘your-message’ => FILTER_SANITIZE_SPECIAL_CHARS,
]);
if (!empty($post_data[‘liifoo_contact_action’]) && $post_data[‘liifoo_contact_action’] === ‘submit’) {
$name = $post_data[‘your-name’] ?? ”;
$email = $post_data[‘your-email’] ?? ”;
$subject = $post_data[‘your-subject’] ?? ‘Contact Form Submission’;
$message = $post_data[‘your-message’] ?? ”;
if (empty($name) || empty($email)) {
wp_die(‘Please fill in all required fields.’);
}
$to = ‘liifooroom@gmail.com’;
$email_subject = ‘[LiiFoo Contact] ‘ . $subject;
$email_body = “Name: $name\nEmail: $email\nSubject: $subject\n\nMessage:\n$message\n\n—\nSent from LiiFoo Contact Form”;
$headers = array(‘From: ‘ . $name . ‘ <' . $email . '>‘, ‘Reply-To: ‘ . $email, ‘Content-Type: text/plain; charset=UTF-8’);
$sent = wp_mail($to, $email_subject, $email_body, $headers);
if ($sent) {
// Auto-reply to the sender
$auto_subject = ‘Thank you for contacting LiiFoo’;
$auto_body = “Dear $name,\n\nThank you for contacting LiiFoo! We have received your message and will get back to you within 24 hours.\n\nBest regards,\nLiiFoo Team\nhttps://www.liifoo.cn/”;
$auto_headers = array(‘From: LiiFoo
wp_mail($email, $auto_subject, $auto_body, $auto_headers);
wp_redirect(add_query_arg(‘contact’, ‘success’, wp_get_referer()));
} else {
wp_redirect(add_query_arg(‘contact’, ‘error’, wp_get_referer()));
}
exit;
}
});
// Display success/error messages
add_action(‘wp_footer’, function() {
if (isset($_GET[‘contact’])) {
if ($_GET[‘contact’] === ‘success’) {
echo ‘
‘;
} elseif ($_GET[‘contact’] === ‘error’) {
echo ‘
‘;
}
}
});
发表回复