Ask any question about WordPress here... and get an instant response.
How can I enqueue scripts and styles properly in my theme?
Asked on Dec 03, 2025
Answer
Enqueuing scripts and styles in WordPress is essential for properly loading assets in your theme without conflicts. This is done using the `wp_enqueue_scripts` action hook in your theme's `functions.php` file.
<!-- BEGIN COPY / PASTE -->
function my_theme_enqueue_scripts() {
// Enqueue a style
wp_enqueue_style('my-style', get_template_directory_uri() . '/css/style.css', array(), '1.0.0', 'all');
// Enqueue a script
wp_enqueue_script('my-script', get_template_directory_uri() . '/js/script.js', array('jquery'), '1.0.0', true);
}
add_action('wp_enqueue_scripts', 'my_theme_enqueue_scripts');
<!-- END COPY / PASTE -->Additional Comment:
- Use `get_template_directory_uri()` for parent themes and `get_stylesheet_directory_uri()` for child themes.
- The last parameter in `wp_enqueue_script` determines whether the script is loaded in the footer (`true`) or the header (`false`).
- Always specify dependencies (e.g., `array('jquery')`) to ensure scripts load in the correct order.
- Version numbers help with cache busting when you update your scripts or styles.
Recommended Links:
