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

WordPress调用w3tc_cache_get()提示函数未定义的问题求助

解决W3 Total Cache中w3tc_cache_get()未定义的错误

问题描述

在WordPress中调用w3tc_cache_get()获取缓存数据时,触发错误:Call to undefined function w3tc_cache_get()。已完成以下检查:

  • W3 Total Cache插件已激活且缓存功能启用
  • 尝试通过require将包含该函数的文件引入主题functions.php
  • WordPress版本为6.2.2,W3 Total Cache为最新版本

原实现代码:

<?php
// Set the expiration time for the cached data.
define('WP_ROCKET_CACHE_EXPIRY', 60 * 60);


// Enable caching for dynamic content.
add_filter('w3tc_cache_dynamic', '__return_true');


// Function to fetch data from an API.
function fetch_api_data($api_url) {

    $api_response = wp_remote_get($api_url);

    if (wp_remote_retrieve_response_code($api_response) === 200) {

        return json_decode(wp_remote_retrieve_body($api_response));

    } else {

        return array();
    
    }
}


function cache_api_data($api_url, $data) {

    $cache_key = 'api_data_' . $api_url;

    w3tc_cache_set($cache_key, $data, 'default', W3TC_CACHE_EXPIRY);
}


function get_cached_data ($api_url) {

    $cache_key = 'api_data_'.$api_url;

    $cached_data = w3tc_cache_get($cache_key, 'default');

    if ($cached_data !== null) return $cached_data;

    return [];

}



function get_api_data ($api_url = "localhost/api/v1/temp_name") {

    $cached_data = get_cached_data ($api_url);

    if ( !empty($cached_data) ) return $cached_data;
    
    $api_response = fetch_api_data ($api_url);

    cache_api_data ($api_url, $api_response);

    return $api_response;
}

核心需求:调用get_api_data()时优先读取缓存,缓存不存在则拉取API数据并写入缓存后返回。

问题分析与修复方案

1. 常量定义笔误

原代码中定义的常量是WP_ROCKET_CACHE_EXPIRY,但调用缓存时使用的是W3TC_CACHE_EXPIRY,会触发未定义常量错误,直接修正常量名称。

2. 手动引入文件的风险

无需手动requireW3TC的函数文件,W3 Total Cache会在插件初始化时自动加载相关函数。手动引入可能因路径错误、加载时机早于插件初始化导致函数未定义,直接移除手动引入代码。

3. 函数存在性检查

在调用w3tc_cache_get()和w3tc_cache_set()前必须检查函数是否存在,避免插件未激活或加载异常时触发错误。

4. 启用W3TC对象缓存

w3tc_cache_get()属于W3TC的对象缓存功能,需在插件设置中启用对象缓存(存储类型可选Disk、Redis等),否则相关函数不会被加载。

修正后的代码

<?php
// 修正常量名称,匹配W3TC使用规范
define('W3TC_CACHE_EXPIRY', 60 * 60);

// 仅当W3TC可用时启用动态缓存
if (function_exists('w3tc_add_filter')) {
    add_filter('w3tc_cache_dynamic', '__return_true');
}

// 拉取API数据的函数
function fetch_api_data($api_url) {
    $api_response = wp_remote_get($api_url);
    
    // 处理请求错误
    if (is_wp_error($api_response)) {
        return [];
    }
    
    if (wp_remote_retrieve_response_code($api_response) === 200) {
        // 转换为数组方便后续处理
        return json_decode(wp_remote_retrieve_body($api_response), true);
    } else {
        return [];
    }
}

// 缓存API数据的函数
function cache_api_data($api_url, $data) {
    if (!function_exists('w3tc_cache_set')) {
        return;
    }
    
    // 用md5处理URL,避免特殊字符导致缓存键异常
    $cache_key = 'api_data_' . md5($api_url);
    w3tc_cache_set($cache_key, $data, 'default', W3TC_CACHE_EXPIRY);
}

// 获取缓存数据的函数
function get_cached_data($api_url) {
    if (!function_exists('w3tc_cache_get')) {
        return [];
    }
    
    $cache_key = 'api_data_' . md5($api_url);
    $cached_data = w3tc_cache_get($cache_key, 'default');
    
    return $cached_data !== null ? $cached_data : [];
}

// 主函数:优先读缓存,无缓存则拉取API数据
function get_api_data($api_url = "localhost/api/v1/temp_name") {
    $cached_data = get_cached_data($api_url);
    
    if (!empty($cached_data)) {
        return $cached_data;
    }
    
    $api_response = fetch_api_data($api_url);
    
    // 仅当API返回有效数据时写入缓存
    if (!empty($api_response)) {
        cache_api_data($api_url, $api_response);
    }
    
    return $api_response;
}

额外验证步骤

  1. 进入W3 Total Cache设置页(Performance → Object Cache),确认对象缓存已启用
  2. 清除W3TC所有缓存后重新测试功能
  3. 确保主题functions.php中已移除手动引入W3TC文件的代码

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.14 12:52:41