【问题标题】:Constantly forcing reload of a page on Wordpress不断强制在 Wordpress 上重新加载页面
【发布时间】:2021-05-28 21:21:08
【问题描述】:

我有一个运行名为 OpenHours 的插件的 Wordpress 站点。基本上,我们将工作时间编程到站点的后端,然后有一个页面/hours/ 显示它们。我已禁用 Wordpress 中所有可能的缓存功能,并尝试使用以下内容编辑 .htaccess 文件:

# DISABLE CACHING
<IfModule mod_headers.c>
    Header set Cache-Control "no-cache, no-store, must-revalidate"
    Header set Pragma "no-cache"
    Header set Expires 0
</IfModule>

<FilesMatch "\.(css|flv|gif|htm|html|ico|jpe|jpeg|jpg|js|mp3|mp4|png|pdf|swf|txt)$">
    <IfModule mod_expires.c>
        ExpiresActive Off
    </IfModule>
    <IfModule mod_headers.c>
        FileETag None
        Header unset ETag
        Header unset Pragma
        Header unset Cache-Control
        Header unset Last-Modified
        Header set Pragma "no-cache"
        Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
        Header set Expires "Thu, 1 Jan 1970 00:00:00 GMT"
    </IfModule>
</FilesMatch>

我们有一个移动应用程序,它基本上通过手机浏览器将用户链接到网页,但只要点击链接,页面就不会更新(插件配置为显示“当前打开”或“当前关闭”基于一天中的时间)。有没有办法强制页面在被访问时更新而不是缓存?

我尝试了一个查询字符串,但不幸的是,我们在应用程序中插入链接的位置不支持任何类型的编码,只是一个直接链接,所以 /hours/?rnd="+Math.random()/hours/?rnd="+new Date().getTime() 不起作用。

【问题讨论】:

    标签: php wordpress caching mobile


    【解决方案1】:

    您可以使用默认的 PHP header 函数为特定页面设置新的标题。

    add_action("init", function() {
        // TODO: Replace with the actual page ID or title
        if(!is_page("page-without-cache")) return;
    
        header("Cache-Control: no-cache, no-store, must-revalidate"); // HTTP 1.1.
        header("Pragma: no-cache"); // HTTP 1.0.
        header("Expires: 0"); // Proxies.
    }, 10);
    
    

    我们使用 init 函数确保在呈现页面之前发送标头。

    如果您使用任何缓存插件,您应该能够创建一个您不想包含或不想被缓存的 URL 或目录的列表。例如,在 WP Super Cache 插件中,该选项位于 Advanced 选项卡 > Accepted Filenames & Rejected URIs 部分。默认情况下,这些路径会从缓存中排除。

    wp-.*\.php
    index\.php
    

    此外,WP 有名为 nocache_headerswp_nocache_headers 的专用函数,您也可以使用它们来防止缓存。这些函数也使用了默认的 PHP 头函数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-05
      • 2016-02-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多