WordPress Q&As Logo
WordPress Q&As Part of the Q&A Network
Q&A Logo

How can I enqueue scripts correctly in my custom WordPress theme?

Asked on Oct 19, 2025

Answer

Enqueuing scripts in WordPress is essential for ensuring that your theme loads JavaScript files properly without conflicts. This is done using the `wp_enqueue_script` function within your theme's `functions.php` file.
<!-- BEGIN COPY / PASTE -->
    function my_theme_enqueue_scripts() {
        wp_enqueue_script('my-custom-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_scripts` action hook to load scripts in the front-end.
  • The third parameter is an array of dependencies (e.g., `array('jquery')`).
  • The fourth parameter is the script version, which can be set to `null` to disable versioning.
  • The fifth parameter is a boolean indicating whether to load the script in the footer (`true`) or the header (`false`).
✅ Answered with WordPress best practices.

← Back to All Questions
The Q&A Network