Ask any question about WordPress here... and get an instant response.
Post this Question & Answer:
What's the best way to enqueue scripts and styles in a WordPress theme?
Asked on Apr 04, 2026
Answer
Enqueuing scripts and styles in a WordPress theme ensures that they are loaded properly and without conflicts. This is done using WordPress functions within the theme's functions.php file.
<!-- BEGIN COPY / PASTE -->
function my_theme_enqueue_scripts() {
wp_enqueue_style('my-theme-style', get_stylesheet_uri());
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:
- Use "wp_enqueue_style" to add stylesheets and "wp_enqueue_script" for JavaScript files.
- Always hook your function to "wp_enqueue_scripts" to ensure scripts and styles are loaded at the correct time.
- Specify dependencies, version numbers, and whether the script should be loaded in the footer.
- Using "get_stylesheet_uri()" loads the main style.css, while "get_template_directory_uri()" is used for other files.
Recommended Links:
