为了加速我的站点加载并防止脚本呈现页面,我正在尝试移动所有possible 脚本(表示JS文件)head
到footer
. 在阅读并进行一些研究后,我编写了以下代码:
function footer_enqueue_scripts() {
remove_action(\'wp_head\', \'wp_print_scripts\');
remove_action(\'wp_head\', \'wp_print_head_scripts\', 9);
remove_action(\'wp_head\', \'wp_enqueue_scripts\', 1);
add_action(\'wp_footer\', \'wp_print_scripts\', 5);
add_action(\'wp_footer\', \'wp_enqueue_scripts\', 5);
add_action(\'wp_footer\', \'wp_print_head_scripts\', 5);
}
add_action(\'after_setup_theme\', \'footer_enqueue_scripts\');
但它不起作用,因为某些脚本仍在加载head
, 请参见以下输出:<head>
<link rel="stylesheet" type="text/css" href="http://elclarin.dev/wp-content/cache/minify/000000/d4587/default.include.993ea9.css" media="all" />
<script type="text/javascript" src="http://elclarin.dev/wp-content/cache/minify/000000/d4587/default.include.0fe0ac.js"></script>
....
<!-- Metas -->
<meta charset="utf-8">
<!-- JS Files -->
<script type="text/javascript" src="http://elclarin.dev/wp-content/themes/elclarin_v2/js/jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="http://elclarin.dev/wp-content/themes/elclarin_v2/js/jquery.tools-1.2.7.min.js"></script>
<script type="text/javascript" src="http://elclarin.dev/wp-content/themes/elclarin_v2/js/prefixfree-1.0.6.min.js"></script>
<script type="text/javascript" src="http://elclarin.dev/wp-content/themes/elclarin_v2/js/modernizr.js"></script>
<!--[if lt IE 9]>
<script type="text/javascript" src="http://elclarin.dev/wp-content/themes/elclarin_v2/js/html5shiv.js"></script>
<![endif]-->
<!--[if (gte IE 6)&(lte IE 8)]>
<script type="text/javascript" src="http://elclarin.dev/wp-content/themes/elclarin_v2/js/selectivizr-1.0.2.min.js"></script>
<![endif]-->
<script type="text/javascript">
var TEMPLATEURL = \'http://elclarin.dev/wp-content/themes/elclarin_v2\';
</script>
<!-- Generated by OpenX 2.8.9 -->
<script type=\'text/javascript\' src=\'http://openx.elclarinweb.com/www/delivery/spcjs.php?id=2&target=_blank\'></script>
<!-- Analytics Files -->
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push([\'_setAccount\', \'UA-29394358-3\']);
_gaq.push([\'_trackPageview\']);
_gaq.push([\'elclarin._setAccount\', \'UA-36592785-1\']);
_gaq.push([\'elclarin._trackPageview\']);
_gaq.push([\'elclarin._setAccount\', \'UA-49334701-1\']);
_gaq.push([\'elclarin._trackPageview\']);
(function () {
var ga = document.createElement(\'script\');
ga.type = \'text/javascript\';
ga.async = true;
ga.src = (\'https:\' == document.location.protocol ? \'https://ssl\' : \'http://www\') + \'.google-analytics.com/ga.js\';
var s = document.getElementsByTagName(\'script\')[0];
s.parentNode.insertBefore(ga, s);
})();
</script>
</head>
...
</footer>
<script type=\'text/javascript\' src=\'http://elclarin.dev/wp-includes/js/admin-bar.min.js?ver=4.1\'></script>
<script type=\'text/javascript\' src=\'http://elclarin.dev/wp-includes/js/jquery/jquery-migrate.min.js?ver=1.2.1\'></script>
<script type=\'text/javascript\' src=\'http://elclarin.dev/wp-includes/js/hoverIntent.min.js?ver=r7\'></script>
...
这个主题有什么变通方法吗?测试用现场为hereUpdate
之后@Milo 提示:我发现他所说的脚本在中的主题中没有正确排队header.php
文件,因为我可以看到:<!-- JS Files -->
<script type="text/javascript" src="<?php echo TEMPLATEURL ?>/js/jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="<?php echo TEMPLATEURL ?>/js/jquery.tools-1.2.7.min.js"></script>
<script type="text/javascript" src="<?php echo TEMPLATEURL ?>/js/prefixfree-1.0.6.min.js"></script>
<script type="text/javascript" src="<?php echo TEMPLATEURL ?>/js/modernizr.js"></script>
<!--[if lt IE 9]>
<script type="text/javascript" src="<?php echo TEMPLATEURL ?>/js/html5shiv.js"></script>
<![endif]-->
<!--[if (gte IE 6)&(lte IE 8)]>
<script type="text/javascript" src="<?php echo TEMPLATEURL ?>/js/selectivizr-1.0.2.min.js"></script>
<![endif]-->
<script type="text/javascript">
var TEMPLATEURL = \'<?php echo TEMPLATEURL; ?>\';
</script>
<script type="text/javascript" src="<?php echo TEMPLATEURL ?>/js/acciones.js"></script>
<!-- Generated by OpenX 2.8.9 -->
<script type=\'text/javascript\' src=\'http://openx.elclarinweb.com/www/delivery/spcjs.php?id=2&target=_blank\'></script>
<!-- Analytics Files -->
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push([\'_setAccount\', \'UA-29394358-3\']);
_gaq.push([\'_trackPageview\']);
_gaq.push([\'elclarin._setAccount\', \'UA-36592785-1\']);
_gaq.push([\'elclarin._trackPageview\']);
_gaq.push([\'elclarin._setAccount\', \'UA-49334701-1\']);
_gaq.push([\'elclarin._trackPageview\']);
(function () {
var ga = document.createElement(\'script\');
ga.type = \'text/javascript\';
ga.async = true;
ga.src = (\'https:\' == document.location.protocol ? \'https://ssl\' : \'http://www\') + \'.google-analytics.com/ga.js\';
var s = document.getElementsByTagName(\'script\')[0];
s.parentNode.insertBefore(ga, s);
})();
</script>
<!-- WP Files -->
<?php wp_head(); ?>
关于这些问题,我的问题包括(我不是主题的开发人员,但我很确定这是可以解决的):在不破坏主题的情况下加载它们,并将性能和页面加速牢记在心的正确方法是什么?