Gửi dữ liệu từ form Forminator về Telegram

Đoạn mã này giúp bạn tích hợp Forminator  với Telegram, tự động gửi dữ liệu form đến một chat Telegram thông qua Token và Chatid khi người dùng submit. Code sử dụng Forminator API để lấy thông tin form và sử dụng ACF (Advanced Custom Fields) để tuỳ biến riêng cho từng trang. Tính năng này rất hữu ích để quản lý thông tin khách hàng từ form liên hệ, đăng ký, hoặc khảo sát một cách nhanh chóng và tiện lợi. Bạn chỉ cần coppy file này rồi bỏ vào funtion là được!
// Chặn truy cập trực tiếp
if (!defined('ABSPATH')) {
    exit;
}

add_action('forminator_custom_form_submit_before_set_fields', 'send_form_data_to_telegram', 10, 3);
function send_form_data_to_telegram($entry, $form_id, $field_data) {
    // Lấy URL và tiêu đề trang gửi form
    $page_url = $_SERVER['HTTP_REFERER'] ?? 'Không xác định';
    $page_title = get_page_title_from_url($page_url);

    // Lấy post ID từ URL và form ID từ ACF
    $post_id = url_to_postid($page_url);
    $acf_form_id = get_field('id_form', $post_id);
    if (!$acf_form_id || $form_id != $acf_form_id) {
        return;
    }

    // Lấy thông tin form từ Forminator
    $form = Forminator_API::get_form($form_id);
    $field_labels = [];
    if ($form && !is_wp_error($form)) {
        foreach ($form->fields as $field) {
            $field_labels[$field->slug] = $field->field_label ?: ucfirst(str_replace('-', ' ', $field->slug));
        }
    }

    // Lấy token và chat ID từ ACF
    $telegram_bot_token = get_field('token_telegram', $post_id);
    $telegram_chat_id = get_field('chat_id_telegram', $post_id);

    // Tạo nội dung tin nhắn Telegram
    $text = " *Form mới từ: $page_title*\n";
    foreach ($field_data as $field) {
        $field_name = $field['name'];
        if (strpos($field_name, '_forminator') === 0) {
            continue;
        }
        $field_value = $field['value'];
        $label = isset($field_labels[$field_name]) ? $field_labels[$field_name] : "Field $field_name";

        if (is_array($field_value)) {
            $field_value = !empty($field_value) ? implode(', ', $field_value) : 'Không chọn';
        } else {
            $field_value = sanitize_text_field($field_value ?: 'Không có dữ liệu');
        }
        $text .= "📌 *$label:* $field_value\n";
    }
    $text .= "🔗 *Trang gửi form:* $page_url";

    // Gửi dữ liệu lên Telegram
    $telegram_url = "https://api.telegram.org/bot" . $telegram_bot_token . "/sendMessage";
    $args = [
        'body' => json_encode([
            'chat_id' => $telegram_chat_id,
            'text' => $text,
            'parse_mode' => 'Markdown'
        ]),
        'headers' => [
            'Content-Type' => 'application/json'
        ],
        'method' => 'POST'
    ];

    $response = wp_remote_post($telegram_url, $args);
    if (is_wp_error($response)) {
        error_log("Lỗi gửi Telegram: " . $response->get_error_message());
    }
}

// Lấy tiêu đề trang từ URL
function get_page_title_from_url($url) {
    $response = wp_remote_get($url);
    if (is_wp_error($response)) {
        return 'Không xác định';
    }
    $html = wp_remote_retrieve_body($response);
    preg_match('/<title>(.*?)<\/title>/', $html, $matches);
    return !empty($matches[1]) ? sanitize_text_field($matches[1]) : 'Không xác định';
}
Chat zalo
Chat whatapp
Liên hệ chúng tôi