Ask any question about WordPress here... and get an instant response.
Post this Question & Answer:
How can I safely enqueue scripts and styles in a WordPress theme?
Asked on Dec 27, 2025
Answer
Enqueuing scripts and styles in WordPress 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_styles() {
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_styles');
<!-- END COPY / PASTE -->Additional Comment:
- Use "wp_enqueue_style" and "wp_enqueue_script" to load styles and scripts, respectively.
- Always hook these functions to "wp_enqueue_scripts" for proper loading.
- Specify dependencies and whether the script should load in the footer (last parameter in "wp_enqueue_script").
- Use "get_stylesheet_uri()" for the main stylesheet and "get_template_directory_uri()" for other assets.
Recommended Links:
