Ask any question about WordPress here... and get an instant response.
How can I enqueue scripts and styles properly in a custom theme?
Asked on Nov 12, 2025
Answer
Enqueuing scripts and styles in a custom WordPress theme ensures that they are loaded correctly and in the right order. This is done using the `wp_enqueue_script` and `wp_enqueue_style` functions within the 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 if the script is loaded in the footer (`true`) or header (`false`).
- Always use `wp_enqueue_scripts` action hook to ensure scripts and styles are loaded properly.
- Version numbers help with cache busting, ensuring users get the latest files.
Recommended Links:
