繁体   English   中英

wp_enqueue 脚本库依赖项

[英]wp_enqueue script library dependancies

我有 6 个库的列表,我想加载到我的 wordpress 插件/主题中。 目前它们都像这样添加到主题中的 header 中。

<script type="text/javascript" src="/wp-content/themes/mytheme/js/jquery.wijmo-open.1.2.0.min.js"></script>

我正在构建一个插件,它有自己的依赖于这些库的脚本。 我想通过我想出的这个解决方案来运行,看看它是否符合 wordpress 标准。 我需要将主题中的脚本排入队列,以便我可以在插件中引用它们。

在主题 header 中,我将所有<script src>更改为<? wp_enqueue_script(); ?> <? wp_enqueue_script(); ?> <? wp_enqueue_script(); ?> .

wp_enqueue_script( 'my-jquery', 'https://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js' );
wp_enqueue_script( 'my-jquery-ui-core', 'https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.12/jquery-ui.min.js' );
wp_enqueue_script( 'raphael', '/wp-content/themes/mytheme/js/external/raphael.js', array('wijmo-jquery','wijmo-jquery-ui-core'));
wp_enqueue_script( 'jquery-glob', '/wp-content/themes/mytheme/js/external/jquery.glob.min.js', array('wijmo-jquery','wijmo-jquery-ui-core'));
wp_enqueue_script( 'jquery-bgiframe', '/wp-content/themes/mytheme/js/external/jquery.bgiframe-2.1.3-pre.js', array('wijmo-jquery','wijmo-jquery-ui-core'));
wp_enqueue_script( 'jquery-mousewheel', '/wp-content/themes/mytheme/js/external/jquery.mousewheel.min.js', array('wijmo-jquery','wijmo-jquery-ui-core'));
wp_enqueue_script( 'wijmo-open', '/wp-content/themes/mytheme/js/jquery.wijmo-open.1.2.0.min.js', array('wijmo-jquery','wijmo-jquery-ui-core','raphael','jquery-glob','jquery-bgiframe'));

在插件中,我现在可以引用我的插件 js 依赖项 jquery-glob、jquery-bgiframe 和 wijmo-open。

wp_enqueue_script( 'wee_broim_download_form', WP_PLUGIN_URL . '/wee-broim-download-form/js/script.js', array('jquery','jquery-ui-core','raphael','jquery-glob','jquery-bgiframe','jquery-mousewheel','wijmo-open','wijmo-complete'));

I understand wordpress has some built in librarys, and that I can use the functions.php to point to jquery, jquery ui cdn's etc, but it works for me just renaming them (my-jquery, my-jquery-ui). 我只是认为我逐步添加依赖项的方式有点混乱。 我希望有办法对我的插件说,好吧,你最后加载。

您不需要显式命名您的 js 文件所依赖的所有库。 例如:

如果库 A 依赖于 B,并且您希望在这 2 之后加载 C,您只需要 C 依赖于 A,因为 A 依赖于 B,最后加载 Z0D61F8370CAD1D412F70B84D143E125。

在你的例子中,你可以做

wp_enqueue_script( 'wee_broim_download_form', WP_PLUGIN_URL . '/wee-broim-download-form/js/script.js', array('jquery-bgiframe','jquery-mousewheel','wijmo-open','wijmo-complete'));

由于wijmo_open取决于您拥有的 rest 库。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM