浅谈SAE上的wordpress优化

2014年8月8日 / 网站源码 / 3条 / 4,710次

修改:

1、剔除了原文章第四个优化,因为修改后会导致白屏,我只好重新下了Wordpress On SAE用程序包的index.php替换了修改过的版本,这才解决了白屏。

2、去除了屏蔽修订的代码,最新版本已经没有修订功能了,用上后反而会给你存修订版本。

3、根据自己的实践有所增减,欢迎评论指出不足或是补充,我会酌情修改。

一、对谷歌字体的处理(3种方法)

众所周知因为国内gfw的关系,谷歌cdn在国内反而会拖慢wordpress加载速度,以下是3种方法。

1、直接禁止谷歌字体的使用

将以下代码加入到当前主题的使用的functions.php即可class Disable_Google_Fonts {
public function __construct() {
add_filter( 'gettext_with_context', array( $this, 'disable_open_sans' ), 888, 4 );
}
public function disable_open_sans( $translations, $text, $context, $domain ) {
if ( 'Open Sans font: on or off' == $context && 'on' == $text ) {
$translations = 'off';
}
return $translations;
}
}
$disable_google_fonts = new Disable_Google_Fonts;
2、更换为国内cdn(推荐)
在主题文件夹下修改functions.php,找到其中的fonts.googleapis.com,替换为fonts.useso.com,这样你就用上了360字体库的缓存。
还有其他的cdn,欢迎尝试:
http://lib.sinaapp.com/
http://developer.baidu.com/wiki/index.php?title=docs/cplat/libs
http://www.staticfile.org/
http://jscdn.upai.com/

3、转存到网站本地(我用的这种)

在主题文件夹下的functions.php中找到fonts.googleapis.com,基本上会是有字体的css地址,以我用的ZeeMinty为例,

找到的是这两处于是我便将这两个文件的代码都复制到一个文件中,命名为font.css,存在主题文件夹下的新建的googlefont文件夹中将两个woff下载,储存在googlefont文件夹中,名字则都是字体名字,修改font.css中的地址,保存接下来就是修改functions.php中的地址,保存即可。

二、删去无用功能

1、禁用WP Cron(我们知道WordPress有个进程专门用来在指定的时间周期或者将来的某个时间点运行特定的任务,比如定时发布某篇日志,这个功能WordPress是通过WP-Cron 实现的。)

将以下代码放入主题的wp-config.php文件中:define('DISABLE_WP_CRON', true);

2、禁用XML-RPC接口(WordPress提供了XML-RPC接口使得第三方的博客写作软件(如windows live writer等)可以与之通信来发布和修改博客。)

将以下代码放入主题的functions.php文件中:add_filter('xmlrpc_enabled', '__return_false');

3、禁用 auto-embeds(oEmbed,oEmbed的功能就是只需要在日志中输入一个视频网站或者图片分享的 URL,这个 URL 里面含有的视频或者图片就自动显示出来,但是很可惜的是只支持部分英文网站,对于中文站来说这个功能是多余的。)

将以下代码放入主题的functions.php文件中:remove_filter( 'the_content', array( $GLOBALS['wp_embed'], 'autoembed' ), 8 );

4、关闭wordpress站内搜索功能以及对外请求(这个酌情吧 我是不干 搜索功能好)

将以下代码放入主题的functions.php文件中:function fb_filter_query( $query, $error = true ) {
if ( is_search() ) {
$query->is_search = false;
$query->query_vars = false;
$query->query = false;
if ( $error == true )
$query->is_404 = true;
}
}
add_action( 'parse_query', 'fb_filter_query' );
add_filter( 'get_search_form', create_function( '$a', "return null;" ) );

三、代码优化1、去掉代码head中冗余代码

将以下代码放入主题的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'));
}

2、强制jquery库文件底部载入(即可强制wordpress的jquery库在底部的wp_footer()钩子中载入了)

将以下代码放入主题的functions.php文件中:

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' );
3、移除 WordPress 加载的JS和CSS链接中的版本号。

将以下代码放入主题的functions.php文件中:

function wpdaxue_remove_cssjs_ver( $src ) {
if( strpos( $src, 'ver=' ) )
$src = remove_query_arg( 'ver', $src );
return $src;
}
add_filter( 'style_loader_src', 'wpdaxue_remove_cssjs_ver', 999 );
add_filter( 'script_loader_src', 'wpdaxue_remove_cssjs_ver', 999 );
4、将博客部分不怎么修改的内容静态化。

以本站About页面为例,复制其所有html代码到站长工具粘贴代码四处按按,一会儿,js代码出来了,复制粘贴到记事本上,以.js后缀保存,编码UTF-8上传到storage上成功用以下代码替换原内容<script src="js地址"></script>比如我传在自己storage的uploads文件夹下那就是<script src="http://babybichu-wordpress.stor.sinaapp.com/uploads/js文件名"></script>操作如下图效果见我的About页面,因为storage由七牛加速自然又省了些云豆了!

四、加速1、CDN

这个对节省云豆有很好的效果,而且起到一定加速作用。

本人用的是七牛,目前storage流量基本没怎么用了,空间配置如下当然需要一个sae专用的七牛插件:插件配置:剩下就是修改其他在你的sae空间下的文件的域名了,看看你的主题在首页、分类页(别在含有评论的类似于有动态参数的页面中找,有个comment-reply.min.js不能换地址)的html代码中那些静态文件是你的sae空间域名下的,找出来,比如http://sae的wp域名/wp-includes/css/dashicons.min.css?ver=3.9、http://sae的wp域名/wp-content/themes/zeeminty/js/navigation.js?ver=3.9这类可以在你wp中的wp-includes/script-loader.php中可以改地址(改法拿这俩的前一个举例,改它我只要在script-loader.php中找到/wp-includes/css/dashicons.min.css替换为http://babybichu1.qiniudn.com/wp-includes/css/dashicons.min.css即可)
像我的font.css还是sae域名的我只要到主题的functions.php下更改就行,同样是加http://babybichu1.qiniudn.com,还剩下一个主题的style.css(连带着主题下的静态文件)真改不了的,便将以下代码放入主题的functions.php文件中://cdn
add_filter('stylesheet_directory_uri','z_cdn_stylesheet_directory_uri',10,3);
function z_cdn_stylesheet_directory_uri($stylesheet_dir_uri, $stylesheet, $theme_root_uri) {
return str_replace(home_url(), CDN_HOST, $stylesheet_dir_uri);
}
add_filter('template_directory_uri','z_cdn_template_directory_uri',10,3);
function z_cdn_template_directory_uri($template_dir_uri, $template, $theme_root_uri)
{
return str_replace(home_url(), CDN_HOST, $template_dir_uri);
}
然后babybichu.me替换成了babybichu.qiniudn.com(storage的cdn),这时我就将对应的主题静态文件上传到storage中呗,比如http://babybichu.qiniudn.com/wp-content/themes/zeeminty/style.css?ver=3.9那我就在storage上建立这样的路径wp-content/themes/(storage目录下建立wp-content文件夹,再在该文件夹下建立themes文件夹),最后将原来sae代码空间上对应的zeeminty文件夹传到themes文件夹中,删除里面的动态文件即可~

2、开启Memcache

这个一般安装SAE版wordpress都有提示开启,设置10就够了,也可以更少,也有加速功能


《浅谈SAE上的wordpress优化》有3 条评论

  1. 帅气小琦琦说道:

    很详细的教程,有些功能已经使用了

  2. 43度说道:

    技术越发成熟了,但是自己的文章少了


提交评论