Wordpress-引用不带'的JS文件;?ver=4.6'

Wordpress - reference JS-file without '?ver=4.6'

本文关键字:ver 文件 引用 Wordpress- JS      更新时间:2023-09-26

在我的WP插件中,我将JS文件放入队列

wp_enqueue_script('myjs', $pluginpath . 'build/js/app.min.js', array('jquery')); 

因此WP生成站点标题:

<script type='text/javascript' src='http://example.com/wp-content/plugins/myplugin/build/js/app.min.js?ver=4.6'></script> 

这个?ver=4.6导致,当我对app.min.js进行更改时,这些更改不会被加载。。相反,"app.min.js"的缓存版本似乎已加载

我该怎么避免呢?

请参阅官方文档。$ver的默认值为false,它将查询字符串设置为您正在使用的WP版本。使用null将其关闭:

// Do not inject query string
wp_enqueue_script('myjs', $pluginpath . 'build/js/app.min.js', array('jquery'), null); 

…或者明确指定一个版本,比如"1.2.3":

// Use custom query string
wp_enqueue_script('myjs', $pluginpath . 'build/js/app.min.js', array('jquery'), '1.2.3'); 
wp_enqueue_script( string $handle, string $src = false, array $deps = array(), string|bool|null $ver = false, bool $in_footer = false )

$ver:

(string|bool|null((可选(指定脚本版本号的字符串(如果有(,它会作为查询字符串添加到URL中,用于破坏缓存。如果version设置为false,则会自动添加与当前安装的WordPress版本相等的版本号如果设置为null,则不添加任何版本

默认值:false所以你应该使用这个:

wp_enqueue_script('myjs', $pluginpath . 'build/js/app.min.js', array('jquery'), null);

查看文档,可以为版本传递值null。这将覆盖默认行为并删除查询字符串。

wp_enqueue_script('myjs', $pluginpath . 'build/js/app.min.js', array('jquery'), null);