cache - Wordpress custom cssjs version not loading correct

admin2025-01-08  6

        $js_version = filemtime(plugin_dir_path(__FILE__) . 'js/script.js');
        $css_version = filemtime(plugin_dir_path(__FILE__) . 'css/style.css');

        $timestamp = time();

        wp_register_style( 'style', plugin_dir_url(__FILE__).'css/style.css', false, $timestamp );
        wp_enqueue_style('style');


        wp_enqueue_script( 'custom_js', plugin_dir_url(__FILE__).'js/script.js', array( 'signature' ), $timestamp, true );

Hi I'm trying to load in a js/css version number in the filepath, so that it does not keep loading old css/js for users, when I update the plugin (it's files). It shows the correct timestamp as ?ver=13434 etc. But it does not update the css/js. Any idea why? I can clear my own cache, but I want it to work on any machine

        $js_version = filemtime(plugin_dir_path(__FILE__) . 'js/script.js');
        $css_version = filemtime(plugin_dir_path(__FILE__) . 'css/style.css');

        $timestamp = time();

        wp_register_style( 'style', plugin_dir_url(__FILE__).'css/style.css', false, $timestamp );
        wp_enqueue_style('style');


        wp_enqueue_script( 'custom_js', plugin_dir_url(__FILE__).'js/script.js', array( 'signature' ), $timestamp, true );

Hi I'm trying to load in a js/css version number in the filepath, so that it does not keep loading old css/js for users, when I update the plugin (it's files). It shows the correct timestamp as ?ver=13434 etc. But it does not update the css/js. Any idea why? I can clear my own cache, but I want it to work on any machine

Share Improve this question asked Nov 7, 2024 at 12:19 Coolguy31Coolguy31 1
Add a comment  | 

1 Answer 1

Reset to default 1

It's not clear if you're trying to load these files on frontend or backend. For the backend I usually do this:

define('MY_PLUGIN_DIR_URL', plugin_dir_url(__FILE__));

//LOAD ADMIN/BACKEND CSS STYLES
add_action('admin_enqueue_scripts', 'my_plugin_enqueue_style_and_scripts', 99);
function my_plugin_enqueue_style_and_scripts() {
    $timestamp = time();
    wp_enqueue_style('my-plugin-backend', MY_PLUGIN_DIR_URL.'/css/style.css', false, $timestamp, 'all');
    wp_enqueue_script( 'my-plugin-backend', MY_PLUGIN_DIR_URL.'/js/script.js', array( 'signature' ), $timestamp, true );
}

You doesn't need to call "plugin_dir_url(FILE)" every time, define a constant once and the code will be more clean.

Concerning your issue: You're saying the source show the properly version in query string but the browser still load the prevoius one?

First of all, disable every caching way in your environment. Some plugins like LiteSpeed use to cache ESI object even in backend.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736270684a1447.html

最新回复(0)