Ask any question about WordPress here... and get an instant response.
How do I enqueue a custom script in a WordPress theme?
Asked on Dec 04, 2025
Answer
To enqueue a custom script in a WordPress theme, you need to use the `wp_enqueue_script` function within your theme's `functions.php` file. This ensures that your script is properly loaded and managed by WordPress.
<!-- BEGIN COPY / PASTE -->
function my_custom_scripts() {
wp_enqueue_script('my-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:
- Replace 'my-custom-script' with a unique handle for your script.
- Update the path '/js/custom-script.js' to the actual location of your script file.
- The array('jquery') parameter ensures jQuery is loaded as a dependency.
- The last parameter 'true' places the script in the footer; use 'false' for the header.
Recommended Links:
