Ask any question about WordPress here... and get an instant response.
Post this Question & Answer:
How can I enqueue scripts correctly in a custom WordPress theme?
Asked on Feb 27, 2026
Answer
Enqueuing scripts in a custom WordPress theme is essential for properly loading JavaScript files without conflicts. This process involves using WordPress functions to ensure scripts are loaded in the correct order and only when needed.
<!-- BEGIN COPY / PASTE -->
function my_theme_enqueue_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_theme_enqueue_scripts');
<!-- END COPY / PASTE -->Additional Comment:
- Use "wp_enqueue_scripts" action to load scripts on the front end.
- Ensure dependencies (e.g., 'jquery') are listed to load them before your script.
- Set the last parameter to "true" to load the script in the footer, improving page load performance.
- Replace "get_template_directory_uri()" with "get_stylesheet_directory_uri()" if using a child theme.
Recommended Links:
