Home » Functions » wp_enqueue_script

WordPress is a powerful content management system that allows users to create amazing websites with ease. One of the most important features of WordPress is the ability to add custom scripts and styles to your website. These scripts and styles can be used to enhance the functionality and appearance of your site.

In order to add scripts and styles to a WordPress site, you will need to use the wp_enqueue_script function. This function allows you to add JavaScript files to your WordPress site in a way that is both efficient and safe. The wp_enqueue_script function ensures that your script files are loaded in the correct order and that there are no conflicts with other scripts on your site.

The wp_enqueue_script function takes three parameters: the handle, the source, and an array of dependencies. The handle is a unique identifier for your script, while the source is the URL of the script file. The dependencies parameter is an array of other scripts that your script relies on.

Here is an example usage code for the wp_enqueue_script function:

function my_scripts() {
    wp_enqueue_script( 'my-custom-script', get_template_directory_uri() . '/js/my-custom-script.js', array( 'jquery' ), '1.0', true );
add_action( 'wp_enqueue_scripts', 'my_scripts' );

In this example, we are adding a custom script file called "my-custom-script.js" to our WordPress site. We are using the handle "my-custom-script" to identify the script and the source parameter to specify the location of the script file. We are also passing an array of dependencies, which includes the jQuery library.

Overall, the wp_enqueue_script function is an essential tool for any WordPress developer who wants to add custom scripts to their site. With this function, you can ensure that your scripts are loaded efficiently and without conflicts, making your site faster, more reliable, and more secure.

Learn More on WordPress.org

Register an account to save your snippets or go Pro to get more features.