Ask any question about WordPress here... and get an instant response.
Post this Question & Answer:
How can I enqueue styles and scripts correctly in a WordPress theme?
Asked on Jan 15, 2026
Answer
To correctly enqueue styles and scripts in a WordPress theme, you should use the `wp_enqueue_style` and `wp_enqueue_script` functions within a function hooked to `wp_enqueue_scripts`. This ensures that your styles and scripts are loaded properly and in the correct order.
<!-- BEGIN COPY / PASTE -->
function my_theme_enqueue_styles_and_scripts() {
// Enqueue a style
wp_enqueue_style('my-theme-style', get_stylesheet_uri());
// Enqueue a script with jQuery dependency
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_styles_and_scripts');
<!-- END COPY / PASTE -->Additional Comment:
- Use `get_stylesheet_uri()` for the main stylesheet of your theme.
- Scripts should be enqueued with dependencies (e.g., `array('jquery')`) to ensure proper loading order.
- The last parameter in `wp_enqueue_script` (set to `true`) loads the script in the footer, which is generally recommended for performance.
- Always hook your enqueue function to `wp_enqueue_scripts` to ensure it runs at the right time.
Recommended Links:
