Skip to main content
Login Join
Snippet · PHP

Add Async to JavaScript

Shared by Mukesh Gujjar · July 9, 2026 · @script_loader_tag

25 views
Back to Snippets

Benefits

Add this into functions.php file

add_filter('script_loader_tag', function ($tag, $handle) {

    $async_scripts = [
        'my-script'
    ];

    if (in_array($handle, $async_scripts)) {
        return str_replace(' src', ' async src', $tag);
    }

    return $tag;

}, 10, 2);
Know a different way to do this? Add your approach as a variation so folks can compare them side by side.
Submit a variation

0 comments