WORDPRESS 5.0 - 6.7+ COMPATIBLE⚡ ความเร็วสูง < 10ms (Transients Caching)🎨 Elementor & Gutenberg Ready

คู่มือการเชื่อมต่อ ThaiGoLive สำหรับ WordPress

เชื่อมต่อผลบอลสด มวยไทย วัวชน IPTV ถ่ายทอดสดสตรีม ความเร็วสูงระดับ Enterprise ด้วยเทคนิค WordPress Transients API ช่วยลดภาระเซิร์ฟเวอร์ โหลดหน้าเว็บเร็วติดสปีด และพร้อมฝังผ่าน Shortcodes หรือ Elementor ได้ทันที

วิธีติดตั้งปลั๊กอินสำเร็จรูป (thaigolive-connector.php)

ไฟล์เดียวจบ ไม่ต้องเขียนโค้ดเอง มีเมนูกรอก API Key ในหลังบ้าน WordPress อัตโนมัติ

ดาวน์โหลด thaigolive-connector.php
1

อัปโหลดไฟล์ปลั๊กอิน

ดาวน์โหลดไฟล์ thaigolive-connector.php แล้วนำไปวางในโฟลเดอร์ wp-content/plugins/thaigolive-connector/ ผ่าน FTP / File Manager หรือบีบอัดเป็น .zip แล้วอัปโหลดผ่านหน้า Admin ปลั๊กอิน

2

เปิดใช้งาน & กรอก API Key

กดเปิดใช้งาน (Activate) ปลั๊กอิน แล้วเข้าไปที่เมนู Settings (การตั้งค่า) > ThaiGoLive API จากนั้นนำ API Key ของคุณมาวางแล้วกดบันทึก

3

วาง Shortcode ในหน้าเว็บ

นำ Shortcode เช่น [tgl_live_matches sport="football"] หรือ [tgl_player match_id="..."] ไปวางใน Elementor หรือหน้าเว็บได้ทันที!

ซอร์สโค้ดปลั๊กอินทั้งหมด (Single-File Plugin):
<?php
/**
 * Plugin Name: ThaiGoLive Multi-Sport API Connector
 * Plugin URI: https://lnwthailive.online/docs
 * Description: ปลั๊กอินเชื่อมต่อ API ผลบอลสด มวยไทย วัวชน IPTV ถ่ายทอดสดสตรีม พร้อมระบบแคช Transient ความเร็วสูง และ Shortcodes สำหรับ WordPress & Elementor
 * Version: 2.1.0
 * Author: LnwthaiLive
 * Author URI: https://lnwthailive.online
 * License: GPL2
 */

if (!defined('ABSPATH')) exit;

// 1. ลงทะเบียนเมนูตั้งค่าใน WP Admin
add_action('admin_menu', function() {
    add_options_page(
        'ตั้งค่า ThaiGoLive API',
        'ThaiGoLive API',
        'manage_options',
        'thaigolive-settings',
        'thaigolive_render_settings_page'
    );
});

add_action('admin_init', function() {
    register_setting('thaigolive_options_group', 'thaigolive_api_key');
    register_setting('thaigolive_options_group', 'thaigolive_api_base');
    register_setting('thaigolive_options_group', 'thaigolive_cache_ttl');
});

function thaigolive_render_settings_page() {
    $apiKey = get_option('thaigolive_api_key', 'YOUR_API_KEY');
    $apiBase = get_option('thaigolive_api_base', 'https://lnwthailive.online');
    $cacheTtl = get_option('thaigolive_cache_ttl', 30);
    ?>
    <div class="wrap" style="max-width: 800px; font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;">
        <h1 style="display: flex; align-items: center; gap: 10px; margin-bottom: 20px;">
            <span style="background: #ea580c; color: #fff; padding: 4px 10px; border-radius: 8px; font-size: 16px;">ThaiGoLive</span>
            ตั้งค่าการเชื่อมต่อ Multi-Sport API
        </h1>

        <div style="background: #fff; border: 1px solid #ccd0d4; border-radius: 12px; padding: 24px; box-shadow: 0 1px 3px rgba(0,0,0,.04);">
            <form method="post" action="options.php">
                <?php settings_fields('thaigolive_options_group'); ?>
                <table class="form-table" role="presentation">
                    <tr>
                        <th scope="row" style="font-weight: 600;">API Key ของคุณ</th>
                        <td>
                            <input type="text" name="thaigolive_api_key" value="<?php echo esc_attr($apiKey); ?>" class="regular-text" style="width: 100%; max-width: 480px; font-family: monospace;" placeholder="เช่น thl_live_xxxxxxx" />
                            <p class="description" style="color: #64748b; margin-top: 6px;">
                                รับ API Key ได้จากทาง Telegram <a href="https://t.me/lnwthaicode" target="_blank">@lnwthaicode</a> หรือดูข้อมูลใน <a href="https://lnwthailive.online/docs" target="_blank">หน้าคู่มือ API Portal</a>
                            </p>
                        </td>
                    </tr>
                    <tr>
                        <th scope="row" style="font-weight: 600;">API Base URL</th>
                        <td>
                            <input type="text" name="thaigolive_api_base" value="<?php echo esc_attr($apiBase); ?>" class="regular-text" style="width: 100%; max-width: 480px; font-family: monospace;" />
                            <p class="description" style="color: #64748b; margin-top: 6px;">ค่ามาตรฐาน: <code>https://lnwthailive.online</code></p>
                        </td>
                    </tr>
                    <tr>
                        <th scope="row" style="font-weight: 600;">ระยะเวลาแคชผลบอลสด (Cache TTL)</th>
                        <td>
                            <input type="number" name="thaigolive_cache_ttl" value="<?php echo esc_attr($cacheTtl); ?>" min="5" max="300" style="width: 120px;" /> วินาที
                            <p class="description" style="color: #64748b; margin-top: 6px;">
                                แนะนำ 15–30 วินาที เพื่อให้หน้าเว็บโหลดเร็วทันใจ (&lt;10ms) และไม่เปลืองโควต้า API
                            </p>
                        </td>
                    </tr>
                </table>

                <div style="margin-top: 24px; padding-top: 20px; border-top: 1px solid #f1f5f9;">
                    <?php submit_button('บันทึกการตั้งค่า', 'primary', 'submit', false); ?>
                </div>
            </form>
        </div>

        <div style="margin-top: 24px; background: #f8fafc; border: 1px solid #e2e8f0; border-radius: 12px; padding: 20px;">
            <h3 style="margin-top: 0; font-size: 15px; color: #0f172a;">📋 ตัวอย่าง Shortcodes ที่พร้อมใช้งานทันที:</h3>
            <ul style="line-height: 1.8; color: #334155; font-size: 13px;">
                <li><code>[tgl_live_matches sport="football" count="10"]</code> : แสดงตารางการแข่งขันฟุตบอลสด</li>
                <li><code>[tgl_live_matches sport="basketball" count="10"]</code> : แสดงตารางการแข่งขันบาสเกตบอล</li>
                <li><code>[tgl_player match_id="104859" responsive="yes"]</code> : ฝังจอถ่ายทอดสดแบบ Responsive 16:9</li>
                <li><code>[tgl_boxing count="5"]</code> : แสดงรายการมวยไทยสด / มวยพักยก</li>
            </ul>
        </div>
    </div>
    <?php
}

// 2. ฟังก์ชันหลักในการเรียก API พร้อมระบบ Caching (Transients API)
function thaigolive_fetch_api($endpoint, $params = array(), $custom_ttl = null) {
    $apiKey = get_option('thaigolive_api_key', 'YOUR_API_KEY');
    $apiBase = rtrim(get_option('thaigolive_api_base', 'https://lnwthailive.online'), '/');
    $ttl = $custom_ttl !== null ? $custom_ttl : intval(get_option('thaigolive_cache_ttl', 30));

    $cache_key = 'tgl_' . md5($endpoint . serialize($params));
    $cached = get_transient($cache_key);
    if (false !== $cached) {
        return $cached;
    }

    $url = $apiBase . $endpoint;
    if (!empty($params)) {
        $url = add_query_arg($params, $url);
    }

    $response = wp_remote_get($url, array(
        'timeout' => 8,
        'headers' => array(
            'x-api-key' => $apiKey,
            'Accept'    => 'application/json',
        ),
    ));

    if (is_wp_error($response)) {
        error_log('[ThaiGoLive API Error]: ' . $response->get_error_message());
        return false;
    }

    $code = wp_remote_retrieve_response_code($response);
    if ($code !== 200) {
        return false;
    }

    $body = wp_remote_retrieve_body($response);
    $data = json_decode($body, true);

    if (!empty($data)) {
        set_transient($cache_key, $data, $ttl);
    }

    return $data;
}

// 3. Shortcode: [tgl_live_matches sport="football" count="10"]
add_shortcode('tgl_live_matches', function($atts) {
    $a = shortcode_atts(array(
        'sport' => 'football',
        'count' => 10,
    ), $atts);

    $endpoint = ($a['sport'] === 'football') 
        ? '/api/public/matches?sport=football' 
        : '/api/ge-sports?type=' . urlencode($a['sport']);

    $res = thaigolive_fetch_api($endpoint, array(), 20);

    $matches = array();
    if (!empty($res['data'])) {
        $matches = $res['data'];
    } elseif (!empty($res['matches'])) {
        $matches = $res['matches'];
    }

    if (empty($matches)) {
        return '<div style="padding:16px;background:#0e101f;color:#94a3b8;border-radius:12px;text-align:center;font-size:13px;border:1px solid rgba(255,255,255,0.06);">ยังไม่มีรายการแข่งขันสดในขณะนี้</div>';
    }

    $output = '<div class="tgl-matches-grid" style="display:flex;flex-direction:column;gap:8px;font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',sans-serif;">';
    $shown = 0;
    foreach ($matches as $m) {
        if ($shown++ >= intval($a['count'])) break;
        $home = esc_html($m['teamHome'] ?? $m['home'] ?? '');
        $away = esc_html($m['teamAway'] ?? $m['away'] ?? '');
        $score = esc_html($m['score'] ?? 'VS');
        $league = esc_html($m['league'] ?? '');
        $isLive = !empty($m['isLive']);
        $statusText = $isLive 
            ? '<span style="color:#22c55e;font-weight:700;font-size:11px;">🔴 สด</span>' 
            : '<span style="color:#64748b;font-size:11px;">' . esc_html($m['time'] ?? 'รอแข่ง') . '</span>';

        $output .= '<div style="background:#0e101f;border:1px solid rgba(255,255,255,0.08);border-radius:12px;padding:12px 16px;display:flex;align-items:center;justify-content:space-between;gap:12px;color:#fff;">';
        $output .= '  <div style="font-size:12px;color:#f97316;font-weight:bold;min-width:90px;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;">' . $league . '</div>';
        $output .= '  <div style="flex:1;display:flex;align-items:center;justify-content:center;gap:12px;font-size:13px;font-weight:600;">';
        $output .= '    <span style="text-align:right;flex:1;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;">' . $home . '</span>';
        $output .= '    <span style="background:rgba(34,197,94,0.15);color:#22c55e;padding:3px 10px;border-radius:8px;font-family:monospace;font-size:12px;font-weight:bold;">' . $score . '</span>';
        $output .= '    <span style="text-align:left;flex:1;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;">' . $away . '</span>';
        $output .= '  </div>';
        $output .= '  <div style="min-width:60px;text-align:right;">' . $statusText . '</div>';
        $output .= '</div>';
    }
    $output .= '</div>';
    return $output;
});

// 4. Shortcode: [tgl_player match_id="104859" height="540" responsive="yes"]
add_shortcode('tgl_player', function($atts) {
    $a = shortcode_atts(array(
        'match_id'   => '',
        'height'     => '540',
        'responsive' => 'yes',
    ), $atts);

    if (empty($a['match_id'])) return '';
    $apiKey = get_option('thaigolive_api_key', 'YOUR_API_KEY');
    $apiBase = rtrim(get_option('thaigolive_api_base', 'https://lnwthailive.online'), '/');
    $url = $apiBase . '/player/iframe?matchId=' . urlencode($a['match_id']) . '&key=' . urlencode($apiKey);

    if ($a['responsive'] === 'yes') {
        return '<div style="position:relative;padding-bottom:56.25%;height:0;overflow:hidden;border-radius:14px;background:#000;box-shadow:0 10px 30px rgba(0,0,0,0.5);">' .
               '<iframe src="' . esc_url($url) . '" style="position:absolute;top:0;left:0;width:100%;height:100%;border:0;" allowfullscreen allow="autoplay; encrypted-media"></iframe>' .
               '</div>';
    }

    return '<div style="width:100%;height:' . intval($a['height']) . 'px;border-radius:14px;overflow:hidden;background:#000;">' .
           '<iframe src="' . esc_url($url) . '" width="100%" height="100%" frameborder="0" allowfullscreen allow="autoplay; encrypted-media"></iframe>' .
           '</div>';
});
?>

คำแนะนำการตั้งค่า Domain Referer Whitelist

ระบบสตรีมถ่ายทอดสดของ LnwthaiLive มีระบบป้องกันการขโมยลิงก์ (Hotlinking Protection) หากคุณนำไปฝังบนเว็บไซต์ของคุณ เบราว์เซอร์จะส่ง HTTP Header Referer หรือ Origin โดยอัตโนมัติ โปรดแจ้งโดเมนเว็บไซต์ของคุณให้แอดมินบันทึกลงในระบบเพื่อการเข้าชมที่ราบรื่น