How can I change my javascript after it has been enqueued? [closed]

Your JS is being cached. In development, just empty your cache. But for production code, it’s helpful to note that wp_enqueue_scripts() takes a version argument that lets you set a script version number which is then added to the URL as a query string for cache busting purposes. (side note, the wp_register_script function is actually built into the wp_enqueue_script function, so you only need the one.)

function registerjs()
{
    wp_enqueue_script('pcjavascript', plugin_dir_url(__FILE__) . 'PerformantCalendar.js', array(), '1.0', false);
}

add_action('wp_enqueue_scripts', 'registerjs');

This will append “?ver=1.0” to the end of the url in your page’s source. Then, after updating the .js file, change the version number:

function registerjs()
{
    wp_enqueue_script('pcjavascript', plugin_dir_url(__FILE__) . 'PerformantCalendar.js', array(), '1.1', false);
}

add_action('wp_enqueue_scripts', 'registerjs');

This will append “?ver=1.1” instead, causing the browser to see it as a different file, and it will request it from the server instead of using a cached copy. More information about wp_enqueue_scripts() can be found here.

Leave a Comment