3

I am new to WordPress and am trying to preload certain css files. I understand that they are loaded into the head using wp_enqueue_style(), so I am trying to access the html generated from that to add rel="preload".

So far it looks like this

wp_enqueue_script('main-style', 'styles/main-style.css', false, null);

add_filter('script_loader_tag', 'preload_filter', 10, 2);

function preload_filter($html, $handle) {
   if (strcmp($handle, 'main-style') == 0) {
      $html = str_replace("rel='stylesheet'", "rel='preload' as='style' ", $html);
   }

   return $html;
} 

Although when I add this preload_filter function my css fails to load completely, and not just for the specified stylesheet... Am I missing anything in trying to do this, or is there a simpler method? Any help would be greatly appreciated.

1
  • Why are you adding main-style.css in wp_enqueue_script() it must be wp_enqueue_style() Commented Mar 15, 2021 at 7:23

2 Answers 2

7
+50

To enqueue style file, you need to use wp_enqueue_style function. You used wp_enqueue_script which is used to enqueue JavaScript file.

wp_enqueue_style('preload-style', 'styles/main-style.css', false, null);

And you need to use style_loader_tag filter to filter the HTML link tag of an enqueued style.

add_filter( 'style_loader_tag',  'preload_filter', 10, 2 );
function preload_filter( $html, $handle ){
    if (strcmp($handle, 'preload-style') == 0) {
        $html = str_replace("rel='stylesheet'", "rel='preload' as='style' onload='this.rel=\"stylesheet\"'", $html);
    }
    return $html;
}

But you used script_loader_tag filter which is used to filter <script> tag.

Sign up to request clarification or add additional context in comments.

6 Comments

Hmm strange, it seems even when I make these changes the css files that I have enqueued aren't loading at all with the preload_function there
You need to load the CSS file later again. Please check developer.mozilla.org/en-US/docs/Web/HTML/Preloading_content In other words, you should have 2 lines (preload and load) in your HTML. <link rel="preload" href="style.css" as="style"> and <link rel="stylesheet" href="style.css"> together. If you can't solve the problem, feel free to DM me.
Ahh okay so I haven't loaded again. So I should wp_enqueue_style again because I have preloaded?
Yes, it's correct. If you face any other issue feel free to contact me via skype (live:rsm0128)
Instead of enqueue style again, you can use onload method. $html = str_replace("rel='stylesheet'", "rel='preload' as='style' onload='this.rel=\"stylesheet\"'", $html);
|
2

(Can't reply, but extends this answer https://stackoverflow.com/a/66640766/1898675)

This modification use the noscript fallback, which is recommended by google for defer css. https://web.dev/defer-non-critical-css/#optimize

add_filter( 'style_loader_tag',  'preload_filter', 10, 2 );
    function preload_filter( $html, $handle ){
    if (strcmp($handle, 'preload-style') == 0) {
        $fallback = '<noscript>' . $html . '</noscript>';
        $preload = str_replace("rel='stylesheet'", "rel='preload' as='style' onload='this.rel=\"stylesheet\"'", $html);
        $html = $preload . $fallback;
    }
    return $html;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.