Ask any question about WordPress here... and get an instant response.
How can I enqueue custom scripts in a WordPress theme?
Asked on Dec 20, 2025
Answer
Enqueuing custom scripts in a WordPress theme is essential for properly adding JavaScript files to your site without conflicts. This is done using the `wp_enqueue_script` function within your 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 ensures scripts are loaded in the correct order and only when necessary.
- Use `get_template_directory_uri()` to correctly reference the theme directory path.
- The `array('jquery')` parameter sets jQuery as a dependency, ensuring it loads first.
- The `true` parameter loads the script in the footer, improving page load performance.
Recommended Links:
