/ 网络资源 / 492 浏览

WordPress修改主题时,强制浏览器更新JS或CSS文件

适合经常折腾 WordPress 主题的博友。

那么,如何让文件版本号自动更新呢?

方法一:最好的办法是用钩子,比如可以这样加载 style.css、script.js 文件和通过文件最后修改时间控制它们的版本:

function theme_scripts() {
	wp_enqueue_style( 'style', get_stylesheet_uri(), array() , filemtime(get_stylesheet_directory().'/style.css'));
	wp_enqueue_script( 'script', get_template_directory_uri().'/js/script.js', array(), filemtime(get_stylesheet_directory().'/js/script.js') , true );
}
add_action( 'wp_enqueue_scripts', 'theme_scripts' );

我现在用的是上面这个代码。

方法二:用当前时间作为版本号

比如,我们可以这样控制主题 style.css 的文件版本。

<link rel="stylesheet" href="//yourdomain.com/wp-content/themes/themename/style.css?v=<?php echo time(); ?>">

但这个方法显然会相当耗费服务器资源。因为每刷新一次页面,就要获取当前时间(unix 格式)下的 css文件版本。

方法三:用文件(最后修改)时间作为版本号

用 filemtime 取得文件修改时间

<link rel="stylesheet" href="//yourdomain.com/wp-content/themes/themename/style.css?ver=<?php echo filemtime(http://yourdomain.com/wp-content/themes/themename/style.css); ?>" />

转自:https://cyhour.com/357/

国内可用的 IPv4 公共加密 DNS:DoH/DoT/DoQ
RegionRestrictionCheck:Linux下,流媒体解锁一键测试脚本
RegionRestrictionCheck:Linux下,流媒体解锁一键测试脚本
流量消耗器:网页版,可自己部署
Linux下,一键更换yum源,一键更换DNS
Linux下,一键更换yum源,一键更换DNS
Super Page Cache for Cloudflare:加速WordPress网站
使用Cloudflare Workers,创建版简易ipinfo
使用Cloudflare Workers,创建版简易ipinfo

0

  1. no comments.

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注