How do I enqueue scripts and styles correctly in a WordPress theme?
Asked on Oct 24, 2025
Answer
Enqueuing scripts and styles in WordPress ensures they are loaded in the correct order and without conflicts. This is done using the `wp_enqueue_script()` and `wp_enqueue_style()` functions within your theme's `functions.php` file.
<!-- BEGIN COPY / PASTE -->
function my_theme_enqueue_scripts() {
// Enqueue a style
wp_enqueue_style('my-style', get_stylesheet_uri());
// Enqueue a script
wp_enqueue_script('my-script', get_template_directory_uri() . '/js/my-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 hook to load both scripts and styles.
- Ensure dependencies are specified in the third parameter of `wp_enqueue_script()` to load scripts in the correct order.
- Set the last parameter of `wp_enqueue_script()` to `true` to load the script in the footer.
- Always use `get_stylesheet_uri()` for the main stylesheet and `get_template_directory_uri()` for other assets.
Recommended Links: