12/19/2023 0 Comments Wp enqueue style cache bust![]() ![]() * Enqueue Scripts & Styles for the back endĪdd_action( 'admin_enqueue_scripts', 'add_assets_admin' ) Wp_enqueue_style( 'nab-css', plugins_url( '/css/style.css', _FILE_ ), array(), $css_ver) $css_ver = date("Ymd-Gis", filemtime( plugin_dir_path( _FILE_ ). Wp_enqueue_script( 'nab-js', plugins_url( '/js/script.js', _FILE_ ), array( 'jquery' ), $js_ver, true) $js_ver = date("Ymd-Gis", filemtime( plugin_dir_path( _FILE_ ). JS (final parameter loads script in footer - recommended) Use cache-busting trick to append version numbers based on file modified time so latest version always loads * Enqueue Scripts & Styles for the front endĪdd_action( 'wp_enqueue_scripts', 'add_assets_client' ) * Description: A test plugin to demonstrate adding css & js files correctly, with optional cache-busting Here is the first bit of my sample plugin in which I correctly load some css and js files for both the front end and back end of a site. So instead of having to update the version # manually to get changes to load reliably, this is automatically taken care of. The example below also includes a trick to prevent browsers from ignoring new versions of the css and js files by automatically setting the file version number based on the last modified time. The correct way to enqueue scripts and styles is to use the hooks ‘wp_enqueue_scripts’ and ‘admin_enqueue_scripts’ hooks, which are executed at the right times to add resources to the front end and back end respectively. ![]() While it usually works, it is not the ‘correct’ way to do it, according to the WordPress documentation and a few tutorials I found out in the wild (including this one). I used to add custom js & css files to WordPress plugins using the hooks init and admin_init, in which I would call wp_enqueue_script() and wp_enqueue_style(). ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |