Writing my first plugin, trying to make it as simple as possible. I put simplistic functions in the javascript just so it would let me know it was there. Afterward I changed the methods in the javascript. WP doesn’t recognize that the javascript changed. It looks like this:
function registerjs()
{
wp_register_script('pcjavascript', plugins_url('PerformantCalendar.js', __FILE__));
//wp_dequeue_script('pcjavascript');
//echo "ichi";
wp_enqueue_script('pcjavascript');
echo "ni";
}
add_action('wp_enqueue_scripts', 'registerjs');
The javascript file looks like this:
function monthNext()
{
alert("In the haunted javascript file");
}
function monthPrev()
{
alert("This also works");
}
Now if I change the message in the alert box, it still alerts by the first message. I’ve tried dequeueing the script, inactivating and reactivating the plugin. I’m missing something. Can somebody help?
1 Answer
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.