Ask any question about WordPress here... and get an instant response.
How can I enqueue a custom script in WordPress?
Asked on Nov 08, 2025
Answer
To enqueue a custom script in WordPress, you can 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'), null, true);
}
add_action('wp_enqueue_scripts', 'my_custom_scripts');
<!-- END COPY / PASTE -->Additional Comment:
- Replace 'my-custom-script' with a unique handle for your script.
- The `get_template_directory_uri()` function retrieves the URL of your theme directory.
- The array('jquery') parameter ensures that jQuery is loaded before your script if it depends on it.
- The `true` parameter at the end loads the script in the footer, which is generally recommended for better performance.
Recommended Links:
