You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Twitch WordPress状态插件故障:无法显示在线/离线状态图片

修复Twitch WordPress状态插件无法识别在线状态的问题

核心问题:Twitch API认证规则已更新

Twitch Helix API目前不再接受仅携带Client-ID的请求,必须同时提供Client-ID和OAuth应用级令牌,这是导致你的请求无效、无法正确获取频道状态的主要原因。

修复步骤及代码调整

1. 获取Twitch应用令牌

登录Twitch开发者控制台,找到你的应用,通过「Client Credentials Flow」生成一个应用令牌(无需用户授权,仅用于应用自身调用API)。

2. 替换并更新插件代码

替换原有的状态获取函数,同时添加缓存、错误日志功能(避免API限流,方便排查问题):

<?php
/*
Plugin Name: Twitch Status - InvidiousAus
Description: Displays an image if the Twitch channel "invidiousaus" is online.
Author: Your Name
Version: 1.1
*/

function smfr_twitch_get_stream_status($channel) {
    // 替换为你的Client ID和生成的应用令牌
    $client_id = 'we57gvgtlmhqnmk44d3mcbc68rgrd1';
    $oauth_token = 'YOUR_TWITCH_APP_TOKEN'; 

    // 缓存状态5分钟,避免频繁调用API触发限流
    $cache_key = "twitch_status_{$channel}";
    $cached_status = get_transient($cache_key);
    if ($cached_status !== false) {
        return $cached_status;
    }

    $api_url = "https://api.twitch.tv/helix/streams?user_login={$channel}";
    $headers = array(
        'Client-ID' => $client_id,
        'Authorization' => "Bearer {$oauth_token}"
    );

    $args = array(
        'headers' => $headers,
        'timeout' => 10
    );

    $response = wp_remote_get($api_url, $args);

    if (is_wp_error($response)) {
        error_log('Twitch API请求失败: ' . $response->get_error_message());
        set_transient($cache_key, 'offline', 60); // 出错时临时缓存离线状态1分钟
        return 'offline';
    }

    $response_code = wp_remote_retrieve_response_code($response);
    $body = wp_remote_retrieve_body($response);

    if ($response_code !== 200) {
        error_log("Twitch API返回错误码: {$response_code}, 响应内容: {$body}");
        set_transient($cache_key, 'offline', 60);
        return 'offline';
    }

    $data = json_decode($body, true);
    if (json_last_error() !== JSON_ERROR_NONE) {
        error_log('Twitch API响应解析失败: ' . json_last_error_msg());
        set_transient($cache_key, 'offline', 60);
        return 'offline';
    }

    $status = !empty($data['data']) ? 'online' : 'offline';
    set_transient($cache_key, $status, 300); // 缓存有效状态5分钟
    return $status;
}

function smfr_twitch_status_fnc() {
    $channel = 'invidiousaus';

    $stream_status = smfr_twitch_get_stream_status($channel);

    // 确认图片路径正确性
    $online_img = plugin_dir_url(__FILE__) . 'img/en_ligne.jpg';
    $offline_img = plugin_dir_url(__FILE__) . 'img/hors_ligne.jpg';

    $html = "<div class='smfr-twitch-status'>";
    $html .= $stream_status === 'online' ? "<img src='{$online_img}' alt='在线'>" : "<img src='{$offline_img}' alt='离线'>";
    $html .= "</div>";

    return $html;
}

function smfr_twitch_status_style() {
    // 如需自定义样式可取消注释
    // wp_enqueue_style('smfr_twitch_status_style', plugin_dir_url(__FILE__) . 'style.css');
}
add_action('wp_enqueue_scripts', 'smfr_twitch_status_style');

add_shortcode('smfr_twitch_status', 'smfr_twitch_status_fnc');

3. 额外检查项

  • 确认插件目录下存在img文件夹,且en_ligne.jpg、hors_ligne.jpg文件存在,文件权限设置正确
  • 开启WordPress调试模式(设置WP_DEBUG=true),查看wp-content/debug.log中的错误日志,排查API请求的具体问题
  • 确保服务器可以正常访问Twitch API地址(无防火墙、代理限制)

内容的提问来源于stack exchange,提问作者Nukleuz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 09:53:14