Ask any question about WordPress here... and get an instant response.
How can I enqueue a custom script in my WordPress theme?
Asked on Dec 17, 2025
Answer
To enqueue a custom script in your WordPress theme, you need to use the `wp_enqueue_script` function within the `functions.php` file of your theme. 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'), '1.0.0', true);
}
add_action('wp_enqueue_scripts', 'my_custom_scripts');
<!-- END COPY / PASTE -->Additional Comment:
- The `wp_enqueue_script` function takes several parameters: handle name, script source, dependencies, version, and whether to load in the footer.
- Ensure your script file exists in the specified path within your theme directory.
- Using `wp_enqueue_scripts` action hook is the recommended way to add scripts and styles in WordPress.
Recommended Links:
