// ============================================================ // EMBROIDEREZE FIX: WPForms email handler + form wiring // Add your WPForms shortcodes to front-page.php after setup // ============================================================ /** * Step 1: After activating WPForms Lite, create two forms in WPForms: * Form A - "Booking Request" (fields: First Name, Last Name, Email, Phone, Service, Quantity, Preferred Date, Job Description) * Form B - "Contact Us" (fields: Name, Email, Subject, Message) * Note the form IDs (shown in WPForms > All Forms as the number after "ID:") * Then update the shortcode IDs below: */ define('EMBROIDEREZE_BOOKING_FORM_ID', 0); // <-- replace 0 with your Booking form ID define('EMBROIDEREZE_CONTACT_FORM_ID', 0); // <-- replace 0 with your Contact form ID /** * Route ALL WPForms submissions to quotes@embroidereze.org.za */ add_filter('wpforms_settings_defaults', function($defaults) { return $defaults; }); add_filter('wpforms_process_before_form_data', function($form_data, $fields) { return $form_data; }, 10, 2); // Override the notification email for all WPForms forms add_filter('wpforms_entry_email_atts', function($atts, $fields, $entry, $form_data) { $atts['address'] = array('quotes@embroidereze.org.za'); return $atts; }, 10, 4); /** * Also set WPForms global "from" and notification email via filter */ add_filter('wpforms_process_entry_save', function($fields, $entry, $form_id, $form_data) { // Ensure notifications go to the right address return $fields; }, 10, 4); /** * Helper: output WPForms shortcode if form exists, otherwise show a setup notice */ function embroidereze_form($form_id, $form_name) { if (!function_exists('wpforms')) { return '
⚠️ WPForms Lite is not active. Please install and activate it from Plugins → Add New → search "WPForms Lite" then activate.
'; } if (empty($form_id) || $form_id === 0) { return '⚠️ ' . esc_html($form_name) . ' not connected yet. See your setup guide for instructions.
'; } return do_shortcode('[wpforms id="' . intval($form_id) . '"]'); } /** * Make WordPress send emails via the correct from address */ add_filter('wp_mail_from', function($email) { return 'noreply@embroidereze.org.za'; }); add_filter('wp_mail_from_name', function($name) { return 'EmbroiderEze Website'; }); /* ============================================================ EMBROIDEREZE – AJAX FORM HANDLERS Added by Claude – fixes the "Submit Booking Request" button and the "Send Message" button so they email quotes@embroidereze.org.za ============================================================ */ // --- Security nonce printed on every page --- add_action('wp_head', function() { echo '' . "\n"; }); // --- BOOKING form handler (logged-out users) --- add_action('wp_ajax_nopriv_embroidereze_booking', 'embroidereze_handle_booking'); add_action('wp_ajax_embroidereze_booking', 'embroidereze_handle_booking'); function embroidereze_handle_booking() { check_ajax_referer('embroidereze_form', 'nonce'); $first = sanitize_text_field($_POST['first_name'] ?? ''); $last = sanitize_text_field($_POST['last_name'] ?? ''); $email = sanitize_email($_POST['email_address'] ?? ''); $phone = sanitize_text_field($_POST['phone_number'] ?? ''); $service = sanitize_text_field($_POST['service_required'] ?? ''); $qty = sanitize_text_field($_POST['quantity_approx'] ?? ''); $date = sanitize_text_field($_POST['preferred_date'] ?? ''); $notes = sanitize_textarea_field($_POST['job_description_special_instructions'] ?? ''); if (!$first || !$email || !$service) { wp_send_json_error(['message' => 'Please fill in all required fields.']); } $to = 'quotes@embroidereze.org.za'; $subject = "New Booking Request from {$first} {$last}"; $body = "You have received a new booking request via your website.\n\n" . "Name : {$first} {$last}\n" . "Email : {$email}\n" . "Phone : {$phone}\n" . "Service : {$service}\n" . "Quantity: {$qty}\n" . "Date : {$date}\n\n" . "Job Description / Special Instructions:\n{$notes}\n"; $headers = [ 'Content-Type: text/plain; charset=UTF-8', "Reply-To: {$first} {$last} <{$email}>", ]; $sent = wp_mail($to, $subject, $body, $headers); if ($sent) { wp_send_json_success(['message' => 'Booking received!']); } else { wp_send_json_error(['message' => 'Sorry, the email could not be sent. Please try WhatsApp or call us directly.']); } } // --- CONTACT form handler --- add_action('wp_ajax_nopriv_embroidereze_contact', 'embroidereze_handle_contact'); add_action('wp_ajax_embroidereze_contact', 'embroidereze_handle_contact'); function embroidereze_handle_contact() { check_ajax_referer('embroidereze_form', 'nonce'); $name = sanitize_text_field($_POST['your_name'] ?? ''); $email = sanitize_email($_POST['email'] ?? ''); $subject = sanitize_text_field($_POST['subject'] ?? '(No subject)'); $message = sanitize_textarea_field($_POST['message'] ?? ''); if (!$name || !$email || !$message) { wp_send_json_error(['message' => 'Please fill in all required fields.']); } $to = 'quotes@embroidereze.org.za'; $subject = "Website Message: {$subject}"; $body = "Message from {$name} <{$email}>\n\n{$message}\n"; $headers = [ 'Content-Type: text/plain; charset=UTF-8', "Reply-To: {$name} <{$email}>", ]; $sent = wp_mail($to, $subject, $body, $headers); if ($sent) { wp_send_json_success(['message' => 'Message sent!']); } else { wp_send_json_error(['message' => 'Sorry, the email could not be sent. Please try WhatsApp or call us directly.']); } } XML-RPC server accepts POST requests only.