Ask any question about WordPress here... and get an instant response.
How can I enqueue styles and scripts properly in a WordPress theme?
Asked on Nov 09, 2025
Answer
Enqueuing styles and scripts in WordPress is essential for maintaining a well-organized and efficient theme. This process ensures that your stylesheets and JavaScript files are loaded correctly and only when needed, preventing conflicts and improving page load times.
<!-- BEGIN COPY / PASTE -->
function my_theme_enqueue_styles() {
wp_enqueue_style('my-style', get_template_directory_uri() . '/style.css');
wp_enqueue_script('my-script', get_template_directory_uri() . '/script.js', array('jquery'), null, true);
}
add_action('wp_enqueue_scripts', 'my_theme_enqueue_styles');
<!-- END COPY / PASTE -->Additional Comment:
- Use "wp_enqueue_scripts" action to enqueue both styles and scripts.
- Ensure dependencies are correctly set, such as including jQuery if needed.
- Set the last parameter in "wp_enqueue_script" to true to load scripts in the footer.
- Always use "get_template_directory_uri()" or "get_stylesheet_directory_uri()" for paths.
Recommended Links:
