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 custom WordPress theme?
Asked on Feb 04, 2026
Answer
Enqueuing scripts and styles in WordPress is essential for ensuring they load correctly and without conflicts. This is done using the `wp_enqueue_scripts` action hook in your theme's `functions.php` file.
<!-- BEGIN COPY / PASTE -->
function my_theme_enqueue_scripts() {
// Enqueue a stylesheet
wp_enqueue_style('my-style', get_template_directory_uri() . '/css/style.css', array(), '1.0.0', 'all');
// Enqueue a script with jQuery as a dependency
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.
- Always specify dependencies and versions to prevent conflicts and ensure proper loading order.
- Set the last parameter of `wp_enqueue_script` to `true` to load scripts in the footer, improving page load times.
Recommended Links:
