Ask any question about WordPress here... and get an instant response.
How can I enqueue custom scripts properly in a WordPress theme?
Asked on Oct 31, 2025
Answer
Enqueuing custom scripts in a WordPress theme ensures that your scripts are loaded correctly and without conflicts. This is done using the `wp_enqueue_script` function within the theme's `functions.php` file.
<!-- BEGIN COPY / PASTE -->
function my_custom_scripts() {
wp_enqueue_script('custom-script', get_template_directory_uri() . '/js/custom-script.js', array('jquery'), null, true);
}
add_action('wp_enqueue_scripts', 'my_custom_scripts');
<!-- END COPY / PASTE -->Additional Comment:
- The `wp_enqueue_script` function registers and enqueues your script.
- The first parameter is a unique handle for the script.
- The second parameter is the path to the script file, often using `get_template_directory_uri()`.
- The third parameter is an array of dependencies, such as `array('jquery')` if your script depends on jQuery.
- The fourth parameter is the script version number, which can be set to `null` to use the theme version.
- The fifth parameter is a boolean indicating whether to load the script in the footer (`true`) or header (`false`).
Recommended Links:
