wp_enqueue script library dependancies
I have a list of 6 library i want to load into my wordpress plugin/theme. Currently they're 开发者_JAVA百科all added to the header in the theme like so.
<script type="text/javascript" src="/wp-content/themes/mytheme/js/jquery.wijmo-open.1.2.0.min.js"></script>
I'm building a plugin that has its own script that depends on these librarys. I want to run by this solution that I come up with to see if it complies with wordpress standards. I needed to en-queue the scripts in the theme, so that I could reference them in the plugin.
In the theme header, i changed all <script src>
to <? 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'));
In the plugin I can now reference my plugin js dependancies jquery-glob, jquery-bgiframe and 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). I just thought the way I'm progressively adding the dependencies was a bit messy. I wish there was a way to say to my plugin, ok, you load last.
You don't need to explicitly name all the libraries that your js file depends on. For example:
If library A depends on B, and you want C to be loaded after these 2, you just need C to depend on A, and as A depends on B, C will be loaded last.
In your example, you can do
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'));
As wijmo_open
depends on the rest libraries you had.
精彩评论