php优化加载速度也,优化wordpress的header.php提高加载速度

wordpress网站速度全研究:优化footer.php

去除不必要php函数

1、<?php bloginfo('rss2_url'); ?> 该函数有些主题会出现在footer.php,是WordPress自带的RSS解释地址函数。由于现在的阅读器一般支持直接输入博客地址自动获取RSS地址,以及很多博客也会通过第三方烧录RSS地址,所以这个函数看个人需要也是可以删除的。

2、<?php bloginfo('comments_rss2_url'); ?>该函数是解释生成评论RSS地址的,如果不提供评论的RSS订阅,那就可以删除这个函数。

3、

该函数是用来显示页面载入速度的,如果显示这个不会让你有多少快感,可以删除。

强制jquery库文件底部载入

function ds_print_jquery_in_footer( &$scripts) {

if ( ! is_admin() )

$scripts->add_data( 'jquery', 'group', 1 );

}

add_action( 'wp_default_scripts', 'ds_print_jquery_in_footer' );

1

2

3

4

5

functionds_print_jquery_in_footer(&$scripts){

if(!is_admin())

$scripts->add_data('jquery','group',1);

}

add_action('wp_default_scripts','ds_print_jquery_in_footer');

通常我们WordPress的jquery是通过wp_head()钩子来载入的,但由于头部载入js是非常影响网站速度的,所以你可以选择把jquery文件强制在底部载入,来优化网站的速度。

header.php中期优化

由于WordPress插件功能的强大之处以及代码修改的折腾,很多使用WordPress程序的朋友都或多或少的使用到了一些插件,不少插件为了实现自己的功能 都需要引用额外的 Javascript 和 CSS 样式,这些内容全部都被自动加载到了页面的 head 标签内,从一定程度上影响到了页面加载的速度。

WordPress插件是根据下面这个函数进行加载的:

1

2

也就是说,如果没有这个函数,插件则不会加载那些内容。我们只需要按照下面的代码写法就能按照自己的需求在文章页(single.php)和页面页 (page.php)内加载相关的插件功能了。

1

2

3

4

如此一来,通过一个简单的判断语句我们就能有效的减少首页的“请求数量”以提高首页的加载速度,又保留了插件的功能。

header.php 优化三   去除一些额外的加载项

header.php默认文件里有很多我们平时做网页时根本不会有的代码,这些代码中,除了调用css或script脚本等必须的几个之外,其余都是可以移除的。下面是美设之家寻找的适合大部分主题的代码

请将下面的代码添加到functions.php里

//移除顶部多余信息

remove_action( 'wp_head', 'wp_enqueue_scripts', 1 ); //Javascript的调用

remove_action( 'wp_head', 'feed_links', 2 ); //移除feed

remove_action( 'wp_head', 'feed_links_extra', 3 ); //移除feed

remove_action( 'wp_head', 'rsd_link' ); //移除离线编辑器开放接口

remove_action( 'wp_head', 'wlwmanifest_link' ); //移除离线编辑器开放接口

remove_action( 'wp_head', 'index_rel_link' );//去除本页唯一链接信息

remove_action('wp_head', 'parent_post_rel_link', 10, 0 );//清除前后文信息

remove_action('wp_head', 'start_post_rel_link', 10, 0 );//清除前后文信息

remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0 );

remove_action( 'wp_head', 'locale_stylesheet' );

remove_action('publish_future_post','check_and_publish_future_post',10, 1 );

remove_action( 'wp_head', 'noindex', 1 );

remove_action( 'wp_head', 'wp_print_styles', 8 );//载入css

remove_action( 'wp_head', 'wp_print_head_scripts', 9 );

remove_action( 'wp_head', 'wp_generator' ); //移除WordPress版本

remove_action( 'wp_head', 'rel_canonical' );

remove_action( 'wp_footer', 'wp_print_footer_scripts' );

remove_action( 'wp_head', 'wp_shortlink_wp_head', 10, 0 );

remove_action( 'template_redirect', 'wp_shortlink_header', 11, 0 );

add_action('widgets_init', 'my_remove_recent_comments_style');

function my_remove_recent_comments_style() {

global $wp_widget_factory;

remove_action('wp_head', array($wp_widget_factory->widgets['WP_Widget_Recent_Comments'] ,'recent_comments_style'));

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

//移除顶部多余信息

remove_action('wp_head','wp_enqueue_scripts',1);//Javascript的调用

remove_action('wp_head','feed_links',2);//移除feed

remove_action('wp_head','feed_links_extra',3);//移除feed

remove_action('wp_head','rsd_link');//移除离线编辑器开放接口

remove_action('wp_head','wlwmanifest_link');//移除离线编辑器开放接口

remove_action('wp_head','index_rel_link');//去除本页唯一链接信息

remove_action('wp_head','parent_post_rel_link',10,0);//清除前后文信息

remove_action('wp_head','start_post_rel_link',10,0);//清除前后文信息

remove_action('wp_head','adjacent_posts_rel_link_wp_head',10,0);

remove_action('wp_head','locale_stylesheet');

remove_action('publish_future_post','check_and_publish_future_post',10,1);

remove_action('wp_head','noindex',1);

remove_action('wp_head','wp_print_styles',8);//载入css

remove_action('wp_head','wp_print_head_scripts',9);

remove_action('wp_head','wp_generator');//移除WordPress版本

remove_action('wp_head','rel_canonical');

remove_action('wp_footer','wp_print_footer_scripts');

remove_action('wp_head','wp_shortlink_wp_head',10,0);

remove_action('template_redirect','wp_shortlink_header',11,0);

add_action('widgets_init','my_remove_recent_comments_style');

functionmy_remove_recent_comments_style(){

global$wp_widget_factory;

remove_action('wp_head',array($wp_widget_factory->widgets['WP_Widget_Recent_Comments'],'recent_comments_style'));

}

上面的代码可以清除WordPress头部大量冗余信息。如有必要,可以看看这些代码的具体意义,以免删除某些你想保留的功能。

2020012309512985.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值