How to enqueue scripts in WordPress from CDN?

Cover Image for How to enqueue scripts in WordPress from CDN?
Matheus Mello
Matheus Mello
published a few days ago. updated a few hours ago

How to Enqueue Scripts in WordPress from CDN: A Complete Guide

šŸ‘‹ Hey, WordPress enthusiasts! If you've ever found yourself wanting to load scripts from a CDN (Content Delivery Network) in your WordPress website, you've come to the right place. In this blog post, we'll walk you through the process of correctly enqueueing scripts from a CDN, so you can enhance your website's performance āš”ļø and make it load faster for your users. Let's get started! šŸš€

Understanding the Problem

šŸ“š The first step is to understand the problem. In this scenario, our fellow developer is trying to replicate a pen from CodePen, which requires loading three script files from a CDN and an additional one from the server. šŸŒ However, they are unsure about the correct syntax to load the scripts from the CDN. Let's take a look at the script files they need to enqueue:

  • jQuery: https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js

  • Slick: https://cdn.jsdelivr.net/jquery.slick/1.5.9/slick.min.js

  • TweenMax: https://cdnjs.cloudflare.com/ajax/libs/gsap/1.18.2/TweenMax.min.js

The Enqueuing Process

āœ… To properly enqueue scripts from a CDN in WordPress, you need to follow these simple steps:

1. Open your theme's functions.php file

šŸ“ Navigate to your WordPress theme's directory and locate the functions.php file. Open it in a text editor of your choice, such as Sublime Text or Visual Studio Code. This is where we'll be adding our code.

2. Use the wp_enqueue_script function

šŸ–Šļø In the functions.php file, you'll need to enqueue each script file using the wp_enqueue_script function. This function allows you to add a script to the list of registered scripts that will be loaded on your website.

Here's an example of how to enqueue the jQuery script from the CDN:

function enqueue_scripts_from_cdn() {
    wp_enqueue_script( 'jquery', 'https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js', array(), '2.1.3', true );
}
add_action( 'wp_enqueue_scripts', 'enqueue_scripts_from_cdn' );

In this example:

  • The first parameter (jquery) is the handle for the script. You can choose any unique name to identify the script.

  • The second parameter (https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js) is the URL of the script file on the CDN.

  • The third parameter (array()) is an array of script dependencies. If your script relies on another script, you can specify it here.

  • The fourth parameter (2.1.3) is the version number of the script. It's important to include a version number to prevent conflicts and ensure compatibility with other scripts or plugins.

  • The fifth parameter (true) is a boolean value indicating whether the script should be loaded in the footer (true) or the header (false) of your website.

šŸ” Repeat this process for each script you want to enqueue from the CDN.

Final Thoughts

šŸŽ‰ Congratulations! You've successfully learned how to enqueue scripts in WordPress from a CDN. By properly enqueuing scripts, you ensure that they are loaded in the correct order and reduce the risk of conflicts with other scripts or plugins.

šŸš€ Remember, using a CDN for scripts can significantly improve your website's performance by serving them from servers located closer to your users. This means faster load times and a better user experience. šŸ’Ŗ

šŸ“¢ Now it's your turn! Have you ever used a CDN for scripts in WordPress? Share your experience in the comments below and let us know if you have any questions or additional tips. And don't forget to share this post with your fellow WordPress developers!

šŸ’Œ Stay tuned for more exciting WordPress tips and tricks on our blog. Happy coding! šŸ’»


More Stories

Cover Image for How can I echo a newline in a batch file?

How can I echo a newline in a batch file?

updated a few hours ago
batch-filenewlinewindows

šŸ”„ šŸ’» šŸ†’ Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Matheus Mello
Matheus Mello
Cover Image for How do I run Redis on Windows?

How do I run Redis on Windows?

updated a few hours ago
rediswindows

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! šŸš€ Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Matheus Mello
Matheus Mello
Cover Image for Best way to strip punctuation from a string

Best way to strip punctuation from a string

updated a few hours ago
punctuationpythonstring

# The Art of Stripping Punctuation: Simplifying Your Strings šŸ’„āœ‚ļø Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Matheus Mello
Matheus Mello
Cover Image for Purge or recreate a Ruby on Rails database

Purge or recreate a Ruby on Rails database

updated a few hours ago
rakeruby-on-railsruby-on-rails-3

# Purge or Recreate a Ruby on Rails Database: A Simple Guide šŸš€ So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? šŸ¤” Well, my

Matheus Mello
Matheus Mello