Các bước gửi dữ liệu contact form 7 lên google sheet không dùng plugin

Bạn muốn lưu trữ thông tin khách hàng từ contact fom 7 lên google google sheet giúp quản lý tốt hơn và dễ chăm sóc khách hàng hơn.có thể làm theo các bước sau nhé:

✅ Bước 1: Tạo Google Sheet

  1. Vào Google Drive → Tạo mới → Google Sheet → Đặt tên: Danh sách đăng ký

  2. Đổi tên sheet/tab (nằm dưới cùng) thành: dangkytuvan

  3. Tạo dòng tiêu đề (tuỳ chọn):

Thời gian | Họ tên | Email | Số điện thoại

✅ Bước 2: Tạo Google Apps Script

  1. Trong Google Sheet → Chọn menu Tiện ích mở rộng → Apps Script

  2. Xóa nội dung mặc định, dán đoạn mã sau:

    </p>
    
    <div>function doPost(e) {</div>
    <div>  const sheet = SpreadsheetApp.openById("1S1jKz-BXoQ8KTSDTNJKKqOtPkcEvL3rVabsbI0Zx-uI")</div>
    <div>    .getSheetByName("dangkytuvan");</div>
    <div></div>
    <div>  if (!sheet) {</div>
    <div>    return ContentService.createTextOutput("Không tìm thấy sheet 'dangkytuvan'");</div>
    <div>  }</div>
    <div></div>
    <div>  const data = e.parameter;</div>
    <div></div>
    <div>  sheet.appendRow([</div>
    <div>    new Date(),</div>
    <div>    data["text-294"] || '',</div>
    <div>    data["email-108"] || '',</div>
    <div>    data["tel-718"] || ''</div>
    <div>  ]);</div>
    <div></div>
    <div>  return ContentService.createTextOutput("Success");</div>
    <div>}</div>
    <p data-start="692" data-end="731">
    

✅ Trong đó:

  • text-294 = họ tên

  • email-108 = email

  • tel-718 = số điện thoại

✅ Trong đó:

text-294 = họ tên

email-108 = email

tel-718 = số điện thoại

https://script.google.com/macros/s/AKfycb.../exec
 

✅ Bước 4: Tạo Contact Form 7
Trong WordPress → Contact Form 7 → Tạo form mới hoặc sửa form cũ, dùng cấu trúc:

plaintext
Copy
Edit

[email* email-108 placeholder “Email”] [tel* tel-718 placeholder “Số điện thoại”] [submit “Gửi”] ✅ Bước 5: Gửi dữ liệu bằng PHP khi submit
Thêm đoạn sau vào file functions.php của theme (hoặc plugin riêng):

add_action('wpcf7_mail_sent', 'gui_dulieu_google_sheets_cf7');
function gui_dulieu_google_sheets_cf7($contact_form) {
    $submission = WPCF7_Submission::get_instance();
    if (!$submission) return;

    $data = $submission->get_posted_data();

    $payload = [
        'text-294'   => sanitize_text_field($data['text-294'] ?? ''),
        'email-108'  => sanitize_email($data['email-108'] ?? ''),
        'tel-718'    => sanitize_text_field($data['tel-718'] ?? '')
    ];

    wp_remote_post('https://script.google.com/macros/s/AKfycb.../exec', [
        'method'  => 'POST',
        'body'    => $payload,
        'headers' => ['Content-Type' => 'application/x-www-form-urlencoded']
    ]);
}

📌 Thay thế đường link https://script.google.com/macros/s/… bằng URL Web App của bạn.

Rate this post

Trả lời

Nhắn tin qua Zalo

0932644183