Seeing as your aim is to improve loading on the front end, the best approach would be to simply not add the defer
attribute when loading in the admin area, like so.
add_filter( 'script_loader_tag', function ( $tag, $handle ) {
if ( 'jquery-core' !== $handle ) {
return $tag;
}
if ( ! is_admin() ) {
$tag = str_replace( ' src', ' defer="defer" src', $tag );
}
return $tag;
}, 10, 2 );
As the editor has a number of scripts, often depending on other scripts already loading, it’s generally not a good idea to try and use async
or defer
in the admin.