Cách gửi dữ liệu từ Contact From 7 về Google sheets không sử dụng Plugin

Cách gửi dữ liệu từ Contact From 7 về Google sheets không sử dụng Plugin

  • Để hạn chế sử dụng plugin và đôi khi sử dụng chung với plugin khác sẽ gây ra xung đột nên mình thấy cách này khá ok và hiệu quả , và kết nối cũng khá đơn giản ! hãy xem kỹ và làm theo nhé
  • Lưu ý là phải cài plugin SMTP vào trước nhé

Hướng dẫn cài SMTP

Video Hướng dẫn :

Code chèn vào file functions.php

add_action('wpcf7_mail_sent', 'send_contact_form_to_google_sheets');

function send_contact_form_to_google_sheets($contact_form) {
    $title = $contact_form->title();
    $submission = WPCF7_Submission::get_instance();
    
    if ($submission) {
        $posted_data = $submission->get_posted_data();
        $contact_form_id = $contact_form->id();
        
        if ($contact_form_id == 1833) {
            $fields = array(
                'entry.524787103' => $submission->get_meta('remote_ip'),
                'entry.94905923' => $posted_data['your-name'],
                // Add other fields similarly
            );

            $url = 'https://docs.google.com/forms/u/0/d/e/1FAIpQLSc6ZyLO5AMNlMr8tvHhWI9k9F8Fotx88qWUx8otOdfKDtIdRg/formResponse';
            
            // Use http_build_query to URL encode the entire string
            $fields_string = http_build_query($fields);

            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

            $result = curl_exec($ch);

            // Check for cURL errors
            if (curl_errno($ch)) {
                // Handle the error, e.g., log it or display a message
                error_log('cURL Error: ' . curl_error($ch));
            }

            curl_close($ch);
        }
    }
}

Giải thích :

  • if ($contact_form_id == 1833) : là cái số id của From bạn thay đổi nhé
  • $fields = array : Các bạn có bao nhiêu Fields thì tạo ra thêm nhé mình có 7 Fields ít hơn thì xoá đi
  • $url : thay link google trang tính của bạn vào

Hãy để lại cảm nhận của bạn về bài viết này

Để lại một bình luận

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *

Press ESC to close