dongsuo9982 2019-02-21 19:54
浏览 100

在Wordpress网站上发布预加载CSS

I am having trouble preloading CSS on my WordPress site. I was writing a script to find every JS and CSS script enqueued in the footer, and preload it in the <head>. The code for the JS scripts works fine (when I go to page source on the site, I can see the <link> tags with the preload attribute inside), but the CSS preload link tags aren't showing up.

It is very likely I am doing this completely wrong, as I found working code for the JS scripts and then tried to alter it to get it to work for the CSS. For instance, I don't think the version appendage applies to CSS, but I assumed it would still work since it would default to false, right?

As a related question, I am having the same issue with webfonts. Google Pageview Insights is telling me to preload webfonts, so I added some php to do that, but when I run pageview insights again, no dice.

Here is the code:

add_action('wp_head', function () {

    global $wp_scripts;
    global $wp_styles;

    foreach($wp_scripts->queue as $handle) {
        $script = $wp_scripts->registered[$handle];

        //-- Check if script is being enqueued in the footer.
        if($script->extra['group'] === 1) {

            //-- If version is set, append to end of source.
            $source = $script->src . ($script->ver ? "?ver={$script->ver}" : "");

            //-- Spit out the tag.
            echo "<link rel='preload' href='{$source}' as='script'/>
";
        }
    }

    foreach($wp_styles->queue as $handle) {
        $style = $wp_styles->registered[$handle];

        if ($style->extra['group'] === 1) {

            $source = $style->src . ($style->ver ? "?ver={$style->ver}" : "");

            echo "<link rel='preload' href='{$source}' as='style' onload='this.rel = \"stylesheet\"'/>

            <noscript><link rel='stylesheet' href='{$source}'/></noscript>
";
        }
    } 
//This is the code to preload webfonts
echo "<link rel='preload' href='/wp-content/themes/salient/css/fonts/fontawesome-webfont.woff?v=4.2' as='font' type='font/woff'>";
echo "<link rel='preload' href='/wp-content/themes/salient/css/fonts/fontawesome-webfont.ttf?v=4.2' as='font' type='font/ttf'>";
}, 1);

</div>
  • 写回答

1条回答 默认 最新

  • doy51723 2019-02-21 20:55
    关注

    Welcome to StackOverflow!

    A function that I have used in my projects for preloading CSS effectively is:

    function add_rel_preload($html, $handle, $href, $media) {
    if (is_admin())
        return $html;
    
    $html = <<<EOT
    <link rel='preload' as='style' onload="this.onload=null;this.rel='stylesheet'" 
    id='$handle' href='$href' type='text/css' media='all' />
    EOT;
    
    return $html;
    }
    
    add_filter( 'style_loader_tag', 'add_rel_preload', 10, 4 );
    

    A similar function could in theory be used for JS and Webfonts, but this has only been tested with CSS.

    Hope this is helpful!

    评论

报告相同问题?

悬赏问题

  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建