Ask any question about WordPress here... and get an instant response.
How do I enqueue custom scripts in a WordPress theme?
Asked on Nov 02, 2025
Answer
To enqueue custom scripts in a WordPress theme, you need to use the `wp_enqueue_script` function within your theme's `functions.php` file. This ensures that your scripts are properly loaded and managed by WordPress.
<!-- BEGIN COPY / PASTE -->
function my_theme_enqueue_scripts() {
wp_enqueue_script('custom-script', get_template_directory_uri() . '/js/custom-script.js', array('jquery'), null, 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.
- Ensure the path to your script is correct (e.g., `/js/custom-script.js`).
- The `array('jquery')` parameter sets jQuery as a dependency, ensuring it loads first.
- The `true` parameter loads the script in the footer for better performance.
Recommended Links:
