Ask any question about WordPress here... and get an instant response.
How can I enqueue scripts and styles properly in my WordPress theme?
Asked on Nov 29, 2025
Answer
To properly enqueue scripts and styles in your WordPress theme, you should use the `wp_enqueue_scripts` action hook. This ensures that your assets are loaded in the correct order and only when needed, improving performance and compatibility.
<!-- BEGIN COPY / PASTE -->
function my_theme_enqueue_scripts() {
// Enqueue a style
wp_enqueue_style('my-theme-style', get_stylesheet_uri());
// Enqueue a script with jQuery as a dependency
wp_enqueue_script('my-theme-script', get_template_directory_uri() . '/js/custom.js', array('jquery'), null, true);
}
add_action('wp_enqueue_scripts', 'my_theme_enqueue_scripts');
<!-- END COPY / PASTE -->Additional Comment:
- Use `get_stylesheet_uri()` for the main stylesheet and `get_template_directory_uri()` for other assets.
- Always specify dependencies and whether the script should be loaded in the footer (last parameter in `wp_enqueue_script`).
- Enqueuing ensures that assets are loaded only once and in the correct order.
- Use version numbers to manage cache busting by passing a version argument.
Recommended Links:
