/* =========================
HANDLE FORM SUBMISSION & EMAIL PDF WITH PASSPORT
========================= */
function cip_handle_submission() {
if (!isset($_POST['cip_submit'])) {
return;
}
// 🔐 Verify nonce
if (
!isset($_POST['cip_nonce']) ||
!wp_verify_nonce($_POST['cip_nonce'], 'cip_form_submit')
) {
return;
}
require_once ABSPATH . 'wp-admin/includes/file.php';
require_once ABSPATH . 'wp-admin/includes/media.php';
require_once ABSPATH . 'wp-admin/includes/image.php';
// =========================
// HANDLE PASSPORT UPLOAD
// =========================
$passport_id = media_handle_upload('passport', 0);
if (is_wp_error($passport_id)) {
return;
}
$passport_path = get_attached_file($passport_id);
// =========================
// SAVE TO DATABASE
// =========================
global $wpdb;
$wpdb->insert(
$wpdb->prefix . 'cip_members',
[
'surname' => sanitize_text_field($_POST['surname'] ?? ''),
'firstname' => sanitize_text_field($_POST['firstname'] ?? ''),
'phone' => sanitize_text_field($_POST['phone'] ?? ''),
'email' => sanitize_email($_POST['email'] ?? ''),
'monthly_contribution' => intval($_POST['monthly_contribution'] ?? 0),
'passport_id' => $passport_id,
'created_at' => current_time('mysql')
],
['%s','%s','%s','%s','%d','%d','%s']
);
// =========================
// LOAD FPDF SAFELY
// =========================
$fpdf_path = plugin_dir_path(__FILE__) . 'fpdf/fpdf.php';
if (!file_exists($fpdf_path)) {
return;
}
require_once $fpdf_path;
// =========================
// CREATE PDF
// =========================
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial', 'B', 14);
$pdf->Cell(0,10,'CIP Cooperative Society – New Member Registration',0,1,'C');
$pdf->Ln(5);
// Passport Image
if (file_exists($passport_path)) {
$pdf->Image($passport_path, 160, 20, 35, 35);
}
$pdf->Ln(20);
$pdf->SetFont('Arial','',12);
$fields = [
'Surname' => $_POST['surname'] ?? '',
'Firstname' => $_POST['firstname'] ?? '',
'Place of Work' => $_POST['workplace'] ?? '',
'Residential Address' => $_POST['res_address'] ?? '',
'Permanent Address' => $_POST['perm_address'] ?? '',
'Phone' => $_POST['phone'] ?? '',
'Email' => $_POST['email'] ?? '',
'Monthly Contribution' => '₦' . intval($_POST['monthly_contribution'] ?? 0),
'Commencement' => $_POST['commencement'] ?? '',
'Next of Kin' => $_POST['kin_name'] ?? '',
'Relationship' => $_POST['relationship'] ?? '',
'Kin Phone' => $_POST['kin_phone'] ?? ''
];
foreach ($fields as $label => $value) {
$pdf->Cell(60,8,$label . ':',0,0);
$pdf->MultiCell(0,8,sanitize_text_field($value));
}
// =========================
// SAVE PDF TEMPORARILY
// =========================
$upload_dir = wp_upload_dir();
$pdf_file = $upload_dir['basedir'] . '/cip-member-' . time() . '.pdf';
$pdf->Output('F', $pdf_file);
// =========================
// SEND EMAIL
// =========================
$to = apply_filters('cip_registration_email', 'info@cipconsulting.com.ng');
$subject = 'New CIP Cooperative Member Registration';
$body = 'A new member has registered. Please find the attached PDF.';
$headers = ['Content-Type: text/html; charset=UTF-8'];
wp_mail($to, $subject, $body, $headers, [$pdf_file]);
// 🧹 Cleanup
if (file_exists($pdf_file)) {
unlink($pdf_file);
}
// =========================
// SUCCESS MESSAGE
// =========================
add_action('wp_footer', function () {
echo "
Registration submitted successfully! A PDF copy including the passport has been sent.
";
});
}
add_action('init', 'cip_handle_submission');
Uncategorized - CIP Consulting
Recent Comments