add_action('wpcf7_mail_sent', 'send_to_onepagecrm'); function send_to_onepagecrm($contact_form) { // 🔒 optionnel : filtrer ton form if ($contact_form->id() != '4de3c4b') return; $submission = WPCF7_Submission::get_instance(); if (!$submission) return; $data = $submission->get_posted_data(); // 🧾 Récupération des champs CF7 $name = sanitize_text_field($data['your-name'] ?? ''); $email = sanitize_email($data['your-email'] ?? ''); $phone = sanitize_text_field($data['your-phone'] ?? ''); $message = sanitize_textarea_field($data['your-message'] ?? ''); if (!$email) return; // 👇 découpe nom/prénom (basique) $parts = explode(' ', $name); $first_name = $parts[0] ?? 'Lead'; $last_name = $parts[1] ?? 'Website'; // 🔐 Auth $user_id = ONEPAGECRM_USER_ID; $api_key = ONEPAGECRM_API_KEY; $auth = base64_encode($user_id . ':' . $api_key); $headers = [ 'Content-Type' => 'application/json', 'Authorization' => 'Basic ' . $auth ]; // 📦 Body dynamique $body = [ "first_name" => $first_name, "last_name" => $last_name, "emails" => [ [ "value" => $email, "type" => "work" ] ], "phones" => $phone ? [ [ "value" => $phone, "type" => "mobile" ] ] : [], "notes" => $message ? [ [ "text" => $message ] ] : [] ]; $response = wp_remote_post( 'https://app.onepagecrm.com/api/v3/contacts.json', [ 'headers' => $headers, 'body' => json_encode($body) ] ); file_put_contents(__DIR__ . '/test-log.txt', print_r($response, true), FILE_APPEND); }