style_loader_tag not changing stylesheet to preload

I am trying to change rel=’stylesheet’ to rel=’preload’ using style_loader_tag but it isn’t doing something. Could anyone please tell me what is wrong with my code?

add_filter( 'style_loader_tag',  'preload_css', 10, 2 );

function preload_css( $html, $handle ){

    $targetHanldes = array('flexible_shipping_notices', 'animate-css');

    if( in_array( $handle, $targetHanldes ) ){

        $html = str_replace("rel='stylesheet'", "rel='preload'", $html);  
    }

    return $html;
}

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

I tested your code and it seems to work fine, my guess is that your $targetHanldes containes the wrong handles.

Best option would be to see what $html and $handle contain and that way you could add them to the haystack.

But for now try this instead

$targetHanldes = array('flexible-shipping-notices', 'animate');

Again this is pure guess work so you better check the $html and $handle variables


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x