• Sabrina B.

    (@sabrinabmoreira)


    Hello
    I created the function below to try to improve the loading of the site both for inclusions in functions and plugin style files, however after implementing it, I’m having duplicate includes.

    function add_async_to_css($html, $handle)
    {
    	$async_loading = array(
    		'structured-content-frontend',
    	);
    
    	if (in_array($handle, $async_loading)) {
            $async_html = str_replace(' href=', 'onload="this.onload=null;this.rel=\'stylesheet\'" href=', $html);
    		$async_html .= str_replace(' rel="stylesheet"', ' rel="preload" as="style" ', $html);
    		return $async_html;
    	}
    	return $html;
    }
    add_filter('style_loader_tag', 'add_async_to_css', 10, 2);

    Result of the inclusion

    <link rel="stylesheet" id="structured-content-frontend-css" onload="this.onload=null;this.rel='stylesheet'" href="http://localhost:5002/wp-content/plugins/structured-content/dist/blocks.style.build.css?ver=1.5.0" media="all">
    
    <link rel="stylesheet" id="structured-content-frontend-css" href="http://localhost:5002/wp-content/plugins/structured-content/dist/blocks.style.build.css?ver=1.5.0" media="all">

    Has anyone had a similar problem?

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    By using .=, you’re creating a duplicate copy of the tag. The first tag was modified the first way, the second modified the second way. Not sure where you’re going with the substitutions, but wouldn’t you apply them all to the same string instead of concatenating another?

Viewing 1 replies (of 1 total)

The topic ‘duplication of inclusions when using function to improve wordpress loading’ is closed to new replies.