How can I enqueue scripts and styles correctly in my custom theme?
Asked on Oct 26, 2025
Answer
Enqueuing scripts and styles in a WordPress theme ensures they are loaded properly and without conflicts. This is typically done in the theme's functions.php file using WordPress functions like wp_enqueue_script and wp_enqueue_style.
<!-- BEGIN COPY / PASTE -->
function my_theme_enqueue_scripts() {
wp_enqueue_style('my-style', get_stylesheet_uri());
wp_enqueue_script('my-script', get_template_directory_uri() . '/js/my-script.js', array('jquery'), null, true);
}
add_action('wp_enqueue_scripts', 'my_theme_enqueue_scripts');
<!-- END COPY / PASTE -->Additional Comment:
- Use wp_enqueue_scripts action to load scripts and styles properly.
- Ensure dependencies are correctly specified in the third parameter of wp_enqueue_script.
- Set the last parameter of wp_enqueue_script to true to load scripts in the footer.
- Use get_stylesheet_uri() for the main stylesheet and get_template_directory_uri() for other files.
Recommended Links: