Ask any question about WordPress here... and get an instant response.
Post this Question & Answer:
How can I enqueue custom scripts and styles in a WordPress theme correctly?
Asked on Feb 22, 2026
Answer
To enqueue custom scripts and styles in a WordPress theme, you should use the `wp_enqueue_scripts` action hook. This ensures that your scripts and styles are loaded properly and in the right order.
<!-- BEGIN COPY / PASTE -->
function my_theme_enqueue_scripts() {
// Enqueue a custom style
wp_enqueue_style('my-custom-style', get_template_directory_uri() . '/css/custom-style.css');
// Enqueue a custom script
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 `get_template_directory_uri()` to get the URL of the theme directory.
- Always use `wp_enqueue_scripts` for front-end scripts and styles.
- Use dependencies (e.g., `array('jquery')`) to ensure scripts load in the correct order.
- Set the last parameter in `wp_enqueue_script` to `true` to load the script in the footer.
Recommended Links:
