How to Enqueue a Script After All Other Scripts in WordPress

By default, WordPress enqueues scripts in the order they are registered. Sometimes a script must load last — after jQuery and all other plugins have added their scripts to the page. Using wp_enqueue_scripts with a high priority and the wp_footer hook gives you full control over script loading order.

Problem: A JavaScript file needs to run after all other scripts on the page — for example, an initialisation script that depends on libraries registered by other plugins — but standard wp_enqueue_script() loads it too early.

Solution: Enqueue the script with in_footer = true and a very high priority (e.g. 9999) on the wp_enqueue_scripts hook, and declare all dependency script handles in the $deps argument so WordPress resolves the correct load order automatically.

The cleanest approach: enqueue in wp_enqueue_scripts with in_footer = true and a very high priority:

// Priority 9999 ensures this runs after most other enqueue callbacks (default = 10)
add_action( 'wp_enqueue_scripts', 'enqueue_last_script', 9999 );

function enqueue_last_script() {
    wp_enqueue_script(
        'my-last-script',
        get_template_directory_uri() . '/js/last.js',
        [ 'jquery' ],     // dependencies — WordPress won't print this until jQuery is printed
        '1.0.0',
        true              // true = load in footer, after all other footer scripts
    );
}

If you need the script to run literally at the very end of the page, hook into wp_footer at a high priority and print it inline:

<?php
add_action( 'wp_footer', 'print_very_last_script', PHP_INT_MAX );

function print_very_last_script() {
    // wp_print_scripts() has already run — this outputs after everything
    ?>
    <script>
        // This runs after all enqueued scripts
        document.dispatchEvent( new Event( 'all-scripts-loaded' ) );
    </script>
    <?php
}

Check the current load order by inspecting all registered hooks on wp_footer:

// Debugging — print all wp_footer hooks and their priorities
add_action( 'wp_footer', function() {
    global $wp_filter;
    $hooks = $wp_filter['wp_footer']->callbacks ?? [];
    ksort( $hooks );
    foreach ( $hooks as $priority => $callbacks ) {
        foreach ( $callbacks as $cb ) {
            $name = is_array( $cb['function'] )
                ? get_class( $cb['function'][0] ) . '::' . $cb['function'][1]
                : ( is_string( $cb['function'] ) ? $cb['function'] : 'closure' );
            echo "<!-- wp_footer priority {$priority}: {$name} -->
";
        }
    }
}, PHP_INT_MAX );

NOTE: The dependency array in wp_enqueue_script() is more reliable than priority tricks for controlling load order between known scripts. If your script depends on jQuery, declare ['jquery'] as a dependency — WordPress will guarantee jQuery is printed before yours, regardless of priority.