Ask any question about WordPress here... and get an instant response.
Post this Question & Answer:
How can I enqueue scripts and styles in a WordPress theme correctly?
Asked on Jan 19, 2026
Answer
To enqueue scripts and styles in a WordPress theme correctly, you should use the `wp_enqueue_scripts` action hook within your theme's `functions.php` file. This ensures that your scripts and styles are loaded properly and only when needed.
<!-- BEGIN COPY / PASTE -->
function my_theme_enqueue_scripts() {
// Enqueue a stylesheet
wp_enqueue_style('my-theme-style', get_stylesheet_uri());
// Enqueue a custom script with jQuery as a dependency
wp_enqueue_script('my-theme-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:
- Always use `wp_enqueue_scripts` for both styles and scripts to ensure proper loading order.
- Use `get_stylesheet_uri()` for the main stylesheet and `get_template_directory_uri()` for other assets.
- Specify dependencies and whether the script should be loaded in the footer (using `true` for the last parameter).
- Enqueuing scripts and styles in this manner helps prevent conflicts and ensures compatibility with other plugins and themes.
Recommended Links:
