How to add external JS code to your WordPress site
WPCodeBox makes it easy to add external JS code to your site. This is useful when you want to include 3rd party libraries from external CDNs.
To enqueue external JS code, we are going to use WordPress native functions. So, first, we create a PHP snippet, and we set it to run on the page we want the external JS code to be included.
The following code will enqueue an external js file:
<?php
add_action('wp_enqueue_scripts', function(){
wp_enqueue_script( '[script_name]', '[script_url]');
});
Make sure to replace the [script_name] with a name of the script (lowercase characters with no spaces, eg new_jquery_version).
Also, [script_url] is the URL to the external script source (eg. https://code.jquery.com/jquery-3.6.0.min.js).
This will allow registering external JS scripts the WordPress way.