When WordPress modifies the theme, force the browser to update JS or CSS files

888u

Last update at :2024-07-06,Edit by888u

Suitable for bloggers who often mess with WordPress themes. So, how to update the file version number automatically?

Method 1: The best way is to use hooks. For example, you can load style.css and script.js files and control their versions through the last modification time of the files:

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' );

I am currently using the above code.

Method 2: Use the current time as the version number

For example, we can control the file version of theme style.css like this.

But this method will obviously consume a lot of server resources. Because every time the page is refreshed, the css file version at the current time (unix format) must be obtained.

Method 3: Use the file (last modification) time as the version number

Use filemtime to get the file modification time

Reprinted from: https://cyhour.com/357/

Recommended site search: domain name resolution server, IP proxy address, cloud host, Korean independent server, latest domain name query, proxy IP address, permanent free cloud server recommendation in mainland China asp free space, online IP check, free asp.net space,

When WordPress modifies the theme, force the browser to update JS or CSS files

All copyrights belong to 888u unless special state
取消
微信二维码
微信二维码
支付宝二维码