LoginSignup
0
0

More than 1 year has passed since last update.

wordpressのCSS、JSのキャッシュクリアのオススメ

Posted at

CSS、JSのキャッシュをクリアする方法は色々あります。CSSを更新したタイミングでのみ、キャッシュクリアする記述方法を見つけたのでメモします。

先にコードから

function theme_enqueue_styles() {
    wp_enqueue_style( 'style', get_stylesheet_directory_uri() . '/style/style.min.css', array(), date("ymdHis", filemtime( get_stylesheet_directory().'/style.css')) );
}
add_action( 'wp_enqueue_scripts', 'theme_enqueue_styles' );

仕組みはwp_enqueue_style関数の第4引数でバージョンを変化させます。対象のファイルの更新日を取得しているのでCSSのファイルを変えたタイミングで更新されるようになっています。
なので、そのページを再読み込みをしてもちゃんとキャッシュが残りますし、あとで、変更などもする必要もないです。
最初は日付でキャッシュクリアをしていました。(あとで消す必要がある)なので、もっと便利な関数を知れてよかったです。

参考

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0