Skip to content

Commit

Permalink
inject directly, not in the footer.
Browse files Browse the repository at this point in the history
  • Loading branch information
aristath committed Dec 18, 2020
1 parent 9aff0aa commit 78f7365
Showing 1 changed file with 27 additions and 33 deletions.
60 changes: 27 additions & 33 deletions lib/client-assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -805,40 +805,34 @@ function() use ( $block_type ) {
* Injects the styles in <head> via JS.
* Styles get added as <link> elements.
*/
add_action(
'wp_footer',
function() {
// Inline a small script to help inject styles in <head>.
?>
<script id="wp-enqueue-style-script">
function wpEnqueueStyle( handle, src, deps, ver, media ) {

// Create the element.
var style = document.createElement( 'link' ),
isFirst = ! window.wpEnqueueStyleLastInjectedEl,
injectEl = isFirst ? document.head : document.getElementById( window.wpEnqueueStyleLastInjectedEl ),
injectPos = isFirst ? 'afterbegin' : 'afterend';

// Add element props for the stylesheet.
style.id = handle + '-css';
style.rel = 'stylesheet';
style.href = src;
if ( ver ) {
style.href += 0 < style.href.indexOf( '?' ) ? '&ver=' + ver : '?ver=' + ver;
}
style.media = media ? media : 'all';

// Set the global var so we know where to add the next style.
// This helps us preserve priorities and inject styles one after the other instead of reversed.
window.wpEnqueueStyleLastInjectedEl = handle + '-css';

// Inject the element.
injectEl.insertAdjacentElement( injectPos, style );
}
</script>
<?php
?>
<script id="wp-enqueue-style-script">
function wpEnqueueStyle( handle, src, deps, ver, media ) {

// Create the element.
var style = document.createElement( 'link' ),
isFirst = ! window.wpEnqueueStyleLastInjectedEl,
injectEl = isFirst ? document.head : document.getElementById( window.wpEnqueueStyleLastInjectedEl ),
injectPos = isFirst ? 'afterbegin' : 'afterend';

// Add element props for the stylesheet.
style.id = handle + '-css';
style.rel = 'stylesheet';
style.href = src;
if ( ver ) {
style.href += 0 < style.href.indexOf( '?' ) ? '&ver=' + ver : '?ver=' + ver;
}
);
style.media = media ? media : 'all';

// Set the global var so we know where to add the next style.
// This helps us preserve priorities and inject styles one after the other instead of reversed.
window.wpEnqueueStyleLastInjectedEl = handle + '-css';

// Inject the element.
injectEl.insertAdjacentElement( injectPos, style );
}
</script>
<?php
$script_added = true;
}

Expand Down

0 comments on commit 78f7365

Please sign in to comment.