So before we enqueue our own version of jQuery, we must de-register the WP version. We could simply enqueue it, but then there would be two copies/versions of jQuery loaded on the page (yours and WP’s). Now, let’s say that we want to use a version of jQuery that is different than the one that is included with WordPress. So when we declare our custom script, we set the $deps parameter equal to array('jquery') and call it a day. Wp_enqueue_script( $handle, $src, $deps, $ver, $in_footer ) For reference, here are the parameters used for wp_enqueue_script(): It isn't necessary but I think it is best practice to enqueue a CDN version of a script with a suffix, i.e. wpenqueuescript( 'jquery' ) will load the local copy of jQuery). This declares jQuery as a dependency for my-custom-script, so WordPress automatically will load its own copy of jQuery. WordPress already has jQuery ready for you to enqueue (i.e. '/js/my-custom-script.js', array('jquery'), null, true) The smart loading requires that you specify jquery as the $deps parameter when enqueuing your other JavaScript files, for example: wp_enqueue_script('my-custom-script', get_template_directory_uri(). These days, WordPress automatically loads jQuery for you, whenever some other script requires it. Much has changed with WP script loading since this article was posted. How was your plugin to know you already had it loaded? For one thing, some plugins use the jQuery library, and they are going to load it as well. ![]() Here’s what you need to do:Īdd the following code to your theme’s functions.If you want, you can just download jQuery, put it on your server and link to it from your header.php file in the section. To load jQuery from a CDN in WordPress, you need to modify your theme or create a plugin. How to Load jQuery from a CDN in WordPress ![]() Reliability: By relying on a large and well-established CDN, you can be confident that the jQuery library will be available and accessible to your users at all times.Better Caching: Loading jQuery from a CDN allows the browser to cache the file, reducing the amount of data that needs to be downloaded every time the user visits your website.When a user visits your website, they will be served the content from the nearest server, reducing the latency and improving the loading time. ![]()
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |