add_action('wp_ajax_process_custom_registration', 'handle_custom_form_registration');
add_action('wp_ajax_nopriv_process_custom_registration', 'handle_custom_form_registration');
function handle_custom_form_registration() {
global $wpdb;
// Define the custom table name
$table_name = $wpdb->prefix . 'custom_registrations';
// 1. Sanitize and capture POST data
$first_name = isset($_POST['first_name']) ? sanitize_text_field($_POST['first_name']) : '';
$last_name = isset($_POST['last_name']) ? sanitize_text_field($_POST['last_name']) : '';
$email = isset($_POST['email']) ? sanitize_email($_POST['email']) : '';
$phone = isset($_POST['phone']) ? sanitize_text_field($_POST['phone']) : '';
$zip = isset($_POST['zip']) ? sanitize_text_field($_POST['zip']) : '';
// Basic required fields validation
if (empty($first_name) || empty($email)) {
wp_send_json_error('Error: Name and Email are required.');
wp_die();
}
// 2. Check if the email already exists in the database
$email_exists = $wpdb->get_var(
$wpdb->prepare(
"SELECT COUNT(*) FROM $table_name WHERE email = %s",
$email
)
);
if ($email_exists > 0) {
// Send the error back to AJAX so the frontend can highlight the duplicate email
wp_send_json_error('Attention: This email is already registered in our system. Please use a different email.');
wp_die();
}
// 3. Insert data into the database table
$inserted = $wpdb->insert(
$table_name,
array(
'first_name' => $first_name,
'last_name' => $last_name,
'email' => $email,
'phone' => $phone,
'zip' => $zip
),
array('%s', '%s', '%s', '%s', '%s')
);
// Stop execution if the database insertion failed
if (!$inserted) {
wp_send_json_error('Error: There was a problem saving your data. Please try again later.');
wp_die();
}
// 4. Path to your HTML templates
$theme_dir = get_stylesheet_directory();
$admin_template_path = $theme_dir . '/emails/email_admin_template.html';
$user_template_path = $theme_dir . '/emails/email_user_template.html';
if (!file_exists($admin_template_path) || !file_exists($user_template_path)) {
wp_send_json_error('Data saved, but email templates are missing on the server.');
wp_die();
}
// 5. Read the content of the HTML templates
$admin_content = file_get_contents($admin_template_path);
$user_content = file_get_contents($user_template_path);
// 6. Replace template variables with user data
$user_content = str_replace('{{first_name}}', $first_name, $user_content);
$user_content = str_replace('{{last_name}}', $last_name, $user_content);
$admin_content = str_replace('{{first_name}}', $first_name, $admin_content);
$admin_content = str_replace('{{email}}', $email, $admin_content);
$admin_content = str_replace('{{phone}}', $phone, $admin_content);
$admin_content = str_replace('{{zip}}', $zip, $admin_content);
// 7. Configure Headers for HTML format and set the Sender (From)
$headers = array(
'Content-Type: text/html; charset=UTF-8',
'From: Florida\'s Voice PAC '
);
// 8. Send the emails using wp_mail
$admin_email = get_option('admin_email'); // Fetches the admin email from WP settings
$admin_subject = 'New Submission: Shevrin Jones for Congress';
$user_subject = 'Thank you for your registration!';
$mail_admin = wp_mail($admin_email, $admin_subject, $admin_content, $headers);
$mail_user = wp_mail($email, $user_subject, $user_content, $headers);
// 9. Return response to Javascript
if ($mail_admin && $mail_user) {
wp_send_json_success('Registration processed successfully.');
} else {
wp_send_json_error('Data saved to database, but there was an issue sending the confirmation emails.');
}
wp_die(); // Always terminate WordPress AJAX functions with wp_die()
}
add_action('wp_ajax_process_custom_unregistration', 'handle_custom_form_unregistration');
add_action('wp_ajax_nopriv_process_custom_unregistration', 'handle_custom_form_unregistration');
function handle_custom_form_unregistration() {
global $wpdb;
// Define the custom table name
$table_name = $wpdb->prefix . 'custom_registrations';
// 1. Sanitize and capture the email sent via POST
$email = isset($_POST['email']) ? sanitize_email($_POST['email']) : '';
if (empty($email) || !is_email($email)) {
wp_send_json_error('Error: A valid email address is required.');
wp_die();
}
// 2. Paths to your HTML templates (Checked FIRST before deleting anything)
$theme_dir = get_stylesheet_directory();
$user_template_path = $theme_dir . '/emails/email_user_template_unsubscriber.html';
$admin_template_path = $theme_dir . '/emails/email_admin_template_unsubscriber.html';
if (!file_exists($user_template_path) || !file_exists($admin_template_path)) {
wp_send_json_error('Error: One or more email templates are missing on the server.');
wp_die();
}
// 3. Check if the email exists in the database
$existing_user = $wpdb->get_row(
$wpdb->prepare(
"SELECT * FROM $table_name WHERE email = %s",
$email
)
);
if (!$existing_user) {
wp_send_json_error('Attention: This email address was not found in our subscriber database.');
wp_die();
}
// 4. Read template contents and replace variables while user data is still available
$user_content = file_get_contents($user_template_path);
$user_content = str_replace('{{first_name}}', $existing_user->first_name, $user_content);
$user_content = str_replace('{{email}}', $email, $user_content);
$admin_content = file_get_contents($admin_template_path);
$admin_content = str_replace('{{first_name}}', $existing_user->first_name, $admin_content);
$admin_content = str_replace('{{last_name}}', isset($existing_user->last_name) ? $existing_user->last_name : '', $admin_content);
$admin_content = str_replace('{{email}}', $email, $admin_content);
$admin_content = str_replace('{{phone}}', isset($existing_user->phone) ? $existing_user->phone : '', $admin_content);
$admin_content = str_replace('{{zip}}', isset($existing_user->zip) ? $existing_user->zip : '', $admin_content);
// 5. Delete the record from the database table only after templates are verified
$deleted = $wpdb->delete(
$table_name,
array('email' => $email),
array('%s')
);
if (!$deleted) {
wp_send_json_error('Error: There was a problem processing your request. Please try again later.');
wp_die();
}
// 6. Configure Headers for HTML format and set the Sender (From)
$headers = array(
'Content-Type: text/html; charset=UTF-8',
'From: Florida\'s Voice PAC '
);
// 7. Send confirmation email to user and notification email to admin
$user_subject = 'Unsubscription Confirmation';
$admin_subject = 'New Unsubscription: Shevrin Jones for Congress';
$admin_email = get_option('admin_email');
$mail_user = wp_mail($email, $user_subject, $user_content, $headers);
$mail_admin = wp_mail($admin_email, $admin_subject, $admin_content, $headers);
// 8. Return response to Javascript
if ($mail_user) {
wp_send_json_success('You have been successfully unsubscribed.');
} else {
wp_send_json_error('Your data was removed, but there was an issue sending the confirmation email.');
}
wp_die();
}
https://floridasvoicepac.click/wp-sitemap-posts-post-1.xmlhttps://floridasvoicepac.click/wp-sitemap-posts-page-1.xmlhttps://floridasvoicepac.click/wp-sitemap-posts-pagelayer-template-1.xmlhttps://floridasvoicepac.click/wp-sitemap-taxonomies-category-1.xmlhttps://floridasvoicepac.click/wp-sitemap-users-1.xml