Ask any question about WordPress here... and get an instant response.
How can I enqueue styles and scripts properly in a WordPress theme?
Asked on Dec 01, 2025
Answer
Enqueuing styles and scripts in a WordPress theme is essential for ensuring that assets are loaded correctly and efficiently. This is done using the `wp_enqueue_style` and `wp_enqueue_script` functions within the theme's `functions.php` file.
<!-- BEGIN COPY / PASTE -->
function my_theme_enqueue_assets() {
// Enqueue a style
wp_enqueue_style('my-theme-style', get_stylesheet_uri());
// Enqueue a script
wp_enqueue_script('my-theme-script', get_template_directory_uri() . '/js/script.js', array('jquery'), null, true);
}
add_action('wp_enqueue_scripts', 'my_theme_enqueue_assets');
<!-- END COPY / PASTE -->Additional Comment:
- Use `get_stylesheet_uri()` to enqueue the main stylesheet.
- Scripts should be enqueued with dependencies (e.g., `array('jquery')`) and set to load in the footer (`true` for the last parameter) for better performance.
- Always use `wp_enqueue_scripts` action to hook your function for enqueuing assets.
- Ensure that paths to your assets are correct, using functions like `get_template_directory_uri()` or `get_stylesheet_directory_uri()`.
Recommended Links:
