Skip to content

Commit dc7915c

Browse files
async/defer scripts
1 parent 3c43d06 commit dc7915c

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

classes/Visualizer/Module/Frontend.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,23 @@ public function __construct( Visualizer_Plugin $plugin ) {
6969
}
7070

7171
add_action( 'rest_api_init', array( $this, 'endpoint_register' ) );
72+
73+
$this->_addFilter( 'script_loader_tag', 'script_loader_tag', null, 10, 3 );
74+
}
75+
76+
/**
77+
* Adds the async attribute to certain scripts.
78+
*/
79+
function script_loader_tag( $tag, $handle, $src ) {
80+
$scripts = array( 'google-jsapi-new', 'google-jsapi-old', 'visualizer-render-google-lib', 'visualizer-render-google' );
81+
82+
foreach ( $scripts as $async ) {
83+
if ( $async === $handle ) {
84+
$tag = str_replace( ' src', ' async="async" defer="defer" src', $tag );
85+
break;
86+
}
87+
}
88+
return $tag;
7289
}
7390

7491
/**

0 commit comments

Comments
 (0)