可以在pluginA中使用wp_script_检查脚本是否正在从pluginB入队/注册吗?

时间:2017-03-29 作者:aberkow

可以wp_script_is 用于pluginA 检查脚本是否正在从中排队/注册pluginB?

我有两个插件,都使用Angular。一个插件(pluginA)具有angular.min.js 正在跨所有页面加载。另一个(pluginB)具有angular.min.js 仅加载一个。我想检查一下pluginA 已排队Angular,但我想从pluginB.

例如,我试过

$handle = \'myAppScript.min.js\' // this is a concatenated/minified version of the entire pluginA angular app.
$list = \'enqueued\' // I also tried all the other $list options.
// if \'myAppScript.min.js\' is present, don\'t register Angular again, otherwise register it.
if (wp_script_is($handle, $list)) {
  return; 
} else {
  wp_register_script( $this->plugin_name . \'-ajs\', plugin_dir_url(__FILE__) . \'js/angular/angular.min.js\', array(), $this->version, false );
}
然而wp_script_is 无论我尝试什么,都返回false(我用var_dump) 因此又增加了角度。

谢谢你的帮助!

更新-我使用is_plugin_active, 但我仍然好奇这个问题是否有答案。谢谢

    $plugin = \'pluginA/pluginA.php\';
    include_once( ABSPATH . \'wp-admin/includes/plugin.php\' );
    if (!is_plugin_active($plugin)) {
      wp_register_script( $this->plugin_name . \'-ajs\', plugin_dir_url(__FILE__) . \'js/angular/angular.min.js\', array(), $this->version, false );
    }

3 个回复
最合适的回答,由SO网友:iyrin 整理而成

现在我看到你plugins_loaded, 这是有道理的,因为它比wp_enqueue_scripts. 预计函数此时将返回0。

看见herehere 通常触发操作的顺序。

尝试add an actionwp_enqueue_scripts 具有延迟优先级,以确保它在其他排队脚本之后运行。那么你可以deregister 脚本并使用新参数再次注册。

如果尝试使用不同的参数注册或排队已注册的句柄,将忽略新参数。相反,请使用wp\\u deregister\\u script()并使用新参数再次注册脚本。

编辑:对不起,我误读了。is_plugin_active 不是动作挂钩,将立即返回true,而不告诉您脚本是否已排队。你必须钩住这个函数wp_script_iswp_enqueue_scripts 或更高版本。

SO网友:Jigar Patel

你走的是正确的道路。没有更好的方法来确定脚本是否已排队。这是一种有效的方法wp_script_is( $handle, $list )

只是想知道您的$句柄名称是否正确。确保$handle 检查wp\\u script\\u时,与将脚本排入pluginB时相同。第一个参数是$handle。请参阅此WP Codex.

上面写着:

参数

$handle

(字符串)(必需)脚本的名称。Should be unique.

我相信您在其他部分中使用插件名称作为句柄。

Try using same name "AngularJS" or something but must be the same as $handle

SO网友:dameer dj

也许我错了,但根据WP codex:

wp_script_is( string $handle, string $list = \'enqueued\' )
$handle - 脚本的唯一名称

据我所知,您使用的是文件名,这是错误的。在我看来,这与什么时候启动操作/挂钩无关,因为你的两个插件都是在什么时候启动的plugins_loaded.

如果你这样看

$handle = \'MY_SCRIPT_NAME\'; /* can\'t really be $this->plugin_name . \'-ajs\' because it gives 2 different strings */
在两个插件中,只会加载所需JS文件的一个实例。

相关推荐

Prevent loading of functions

我使用了一个主题,其中包括名为“Sharethis”的服务的共享功能和字体真棒。据我所知,函数是从函数中的以下行加载的。php。TT\\u队列::add\\u css(数组(“//netdna.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css”);TT\\u排队::add\\u js(array(\'https://ws.sharethis.com/button/buttons.js\'));如何使用子函数(因为父函数中的更改将在更新