Ask any question about WordPress here... and get an instant response.
How can I enqueue custom scripts properly in a WordPress theme?
Asked on Dec 05, 2025
Answer
To enqueue custom scripts in a WordPress theme, you should use the `wp_enqueue_script` function within a theme's `functions.php` file. This ensures that your scripts are loaded correctly and in the right order.
<!-- BEGIN COPY / PASTE -->
function my_theme_enqueue_scripts() {
wp_enqueue_script('custom-script', get_template_directory_uri() . '/js/custom-script.js', array('jquery'), '1.0.0', true);
}
add_action('wp_enqueue_scripts', 'my_theme_enqueue_scripts');
<!-- END COPY / PASTE -->Additional Comment:
- Place the code in your theme's `functions.php` file.
- The `wp_enqueue_script` function parameters include a handle name, the script's URL, dependencies, version number, and whether to load in the footer.
- Using `wp_enqueue_scripts` action ensures scripts are loaded at the right time.
- Ensure your script path is correct relative to your theme directory.
Recommended Links:
