Published on 09/16/2023
Published by amit
Adding 'Defer' attribute to Scripts added by Plugins in WordPress
How to add ‘defer‘ to scripts added by plugins in WordPress.
1 2 3 4 5 6 7 8 9 10 11 |
function wpoets_defer_scripts( $tag, $handle, $src ) { $defer = array( 'cff_carousel_js', 'cffscripts', 'ubermenu' ); if ( in_array( $handle, $defer ) ) { return '<script src="' . $src . '" defer="defer" type="text/javascript"></script>' . "\n"; } return $tag; } add_filter( 'script_loader_tag', 'wpoets_defer_scripts', 10, 3 ); |
In this example, we are adding ‘defer’ ubermenu, cffscripts, and cff_carousel_js javascript files.