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 02, 2025
Answer
Enqueuing custom scripts in a WordPress theme ensures that scripts are loaded correctly and efficiently. This is done using the `wp_enqueue_script` function within the theme's `functions.php` file.
<!-- BEGIN COPY / PASTE -->
function my_custom_scripts() {
wp_enqueue_script('my-script', get_template_directory_uri() . '/js/my-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 registers and enqueues a script in WordPress.
- Parameters include the script handle, source URL, dependencies, version number, and whether to load in the footer.
- Using `wp_enqueue_scripts` action hook ensures scripts are loaded at the right time in the page lifecycle.
Recommended Links:
