Skip to content

Enqueue scripts in development mode

Enqueue scripts in development

Imagine, you have done so many complex works doing the changes in your CSS file or JS file, and the changes are not reflected in your browser. This situation often takes our valuable time away. But you can do a trick and enqueue scripts in the development of a WordPress site.

Browsers do not always load the same file twice until we force them to do so. For WordPress sites, the browser checks the version of your script first. If only the version is changed, the browser will reload the file. Here is a trick for your development environment –


wp_enqueue_script(
    "name",
    get_stylesheet_directory_uri() . "/path/to/your/file.js",
    array('jquery'), //dependencies,
    microtime(), //everytime version will be changed
    true, //in footer or not
);

Here, I have used microtome() function for the version of the script while I enqueue scripts in development. So every time the page reloads, the JS file will be reloaded for sure. And all of your changes will be fetched. Using this trick in your development site will be very helpful. But remember to remove it from the live server.

By using this trick, you can just refresh the browser instead of a hard refresh each time. Thus there will be no wasted time and you can be sure that every change you made during the development, will be fetched correctly.

Learn more about custom JS

Category: JavaScript

Free WP tutorials – Learn Web Code


Leave a Reply