Ask any question about WordPress here... and get an instant response.
Post this Question & Answer:
How can I enqueue custom scripts in a WordPress theme?
Asked on Jan 27, 2026
Answer
To enqueue custom scripts in a WordPress theme, you should use the `wp_enqueue_script` function within a hook that runs during the WordPress initialization process. This ensures that your scripts are loaded properly and in the correct order.
<!-- 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:
- Place the code in your theme's `functions.php` file.
- The `wp_enqueue_scripts` action hook is used to enqueue both styles and scripts.
- The `array('jquery')` parameter specifies dependencies, ensuring jQuery loads before your script.
- The `true` parameter loads the script in the footer, which is generally recommended for performance.
Recommended Links:
