The WPFolks app is out on iPhone and Android. Free, no ads. Download on the App Store Get it on Google Play
Skip to main content
Login Join
Snippet · PHP

Add Defer to JavaScript

Shared by Mukesh Gujjar · July 9, 2026

29 views
Back to Snippets
add_filter('script_loader_tag', function ($tag, $handle) {

    $defer = [
        'my-script'
    ];

    if (in_array($handle, $defer)) {
        return str_replace(' src', ' defer 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