wordpress太卡,各方面优化指南

请注意,本文编写于 106 天前,最后修改于 6 天前,其中某些信息可能已经过时。

每一个wordpress深度用户患者,无不是折腾帝。在优化wordpress方面大家各自有自己的一套理论经验,在这里简单分享下我对wordpress的一些优化

一、删除无用功能及硬性优化

在主题functions.php里添加如下内容

/* 替换Gravatar为v2ex头像源*/
function mytheme_get_avatar( $avatar ) {
    $avatar = preg_replace("/http:\/\/(www|\d).gravatar.com\/avatar\//","http://cdn.v2ex.com/gravatar/",$avatar);
    return $avatar;
}
add_filter('get_avatar', 'mytheme_get_avatar');

//禁用更新提醒
add_filter('pre_site_transient_update_core', create_function('$a', "return null;")); // 关闭核心提示
add_filter('pre_site_transient_update_plugins', create_function('$a', "return null;")); // 关闭插件提示
add_filter('pre_site_transient_update_themes', create_function('$a', "return null;")); // 关闭主题提示
remove_action('admin_init', '_maybe_update_core'); // 禁止 WordPress 检查更新
remove_action('admin_init', '_maybe_update_plugins'); // 禁止 WordPress 更新插件
remove_action('admin_init', '_maybe_update_themes'); // 禁止 WordPress 更新主题
/*七牛云加速*/

define('FocusCDNHost','http://devoted.net.cn');//wordpress网站网址
define('FocusCDNRemote','http://cdn.****');//cdn域名
define('FocusCDNIncludes','wp-content,wp-includes');//设置加速目录
define('FocusCDNExcludes','.php|.xml|.html|.po|.mo|.js|.css|.mp4');//设置文件白名单
define('FocusCDNRelative','');//Check this if you want to have links like <wp-content/abc.png> rewritten - i.e. without your blog's domain as prefix.

function do_cdnrewrite_ob_start() {
$rewriter = new FocusCDNRewriteWordpress();
$rewriter->register_as_output_buffer();
}
add_action('template_redirect', 'do_cdnrewrite_ob_start');

class FocusCDNRewriteWordpress extends FocusCDNRewrite
{
function __construct() {
$excl_tmp = FocusCDNExcludes;
$excludes = array_map('trim', explode('|', $excl_tmp));

parent::__construct(
FocusCDNHost,
FocusCDNRemote,
FocusCDNIncludes,
$excludes,
!!FocusCDNRelative
);
}
public function register_as_output_buffer() {
if ($this->blog_url != FocusCDNRemote) {
ob_start(array(&$this, 'rewrite'));
}
}

}

class FocusCDNRewrite {
var $blog_url    = null;
var $cdn_url     = null;
var $include_dirs   = null;
var $excludes    = array();
var $rootrelative   = false;

function __construct($blog_url, $cdn_url, $include_dirs, array $excludes, $root_relative) {
$this->blog_url   = $blog_url;
$this->cdn_url    = $cdn_url;
$this->include_dirs  = $include_dirs;
$this->excludes   = $excludes;
$this->rootrelative  = $root_relative;
}

protected function exclude_single(&$match) {
foreach ($this->excludes as $badword) {
if (stristr($match, $badword) != false) {
return true;
}
}
return false;
}

protected function rewrite_single(&$match) {
if ($this->exclude_single($match[0])) {
return $match[0];
} else {
if (!$this->rootrelative || strstr($match[0], $this->blog_url)) {
return str_replace($this->blog_url, $this->cdn_url, $match[0]);
} else {
return $this->cdn_url . $match[0];
}
}
}

protected function include_dirs_to_pattern() {
$input = explode(',', $this->include_dirs);
if ($this->include_dirs == '' || count($input) < 1) {
return 'wp\-content|wp\-includes';
} else {
return implode('|', array_map('quotemeta', array_map('trim', $input)));
}
}

public function rewrite(&$content) {
$dirs = $this->include_dirs_to_pattern();
$regex = '#(?<=[(\"\'])';
$regex .= $this->rootrelative
? ('(?:'.quotemeta($this->blog_url).')?')
: quotemeta($this->blog_url);
$regex .= '/(?:((?:'.$dirs.')[^\"\')]+)|([^/\"\']+\.[^/\"\')]+))(?=[\"\')])#';
return preg_replace_callback($regex, array(&$this, 'rewrite_single'), $content);
}

}
/*反垃圾*/
function refused_spam_comments($comment_data) {
    $pattern = '/[一-龥]/u';
    $jpattern = '/[ぁ-ん]+|[ァ-ヴ]+/u';
    if (!preg_match($pattern, $comment_data['comment_content'])) {
        err(__('来一波汉字吧,苦逼的庭主只认识汉字!You should type some Chinese word!'));
    }
    if (preg_match($jpattern, $comment_data['comment_content'])) {
        err(__('原谅庭主吧,这货只听得懂岛国神片的一两句雅蠛蝶 Japanese Get out!日本语出て行け! You should type some Chinese word!'));
    }
    return ($comment_data);
}
    add_filter('preprocess_comment', 'refused_spam_comments');
function Shield_link($comment_data) {
    $links = '/http:\/\/|https:\/\/|www\./u';
    if (preg_match($links, $comment_data['comment_author']) || preg_match($links, $comment_data['comment_content'])) {
        err(__('别啊,昵称和评论里面添加链接会怀孕的哟!!'));
    }
    return ($comment_data);
}
    add_filter('preprocess_comment', 'Shield_link');
function lang_url_spamcheck($approved, $commentdata) {
    return (strlen($commentdata['comment_author_url']) > 50) ?
    'spam' : $approved;
}
add_filter('pre_comment_approved', 'lang_url_spamcheck', 99, 2);
//删除emoji脚本
remove_action( 'admin_print_scripts',   'print_emoji_detection_script');
remove_action( 'admin_print_styles',    'print_emoji_styles');
remove_action( 'wp_head',       'print_emoji_detection_script', 7);
remove_action( 'wp_print_styles',   'print_emoji_styles');
remove_filter( 'the_content_feed',  'wp_staticize_emoji');
remove_filter( 'comment_text_rss',  'wp_staticize_emoji');
remove_filter( 'wp_mail',       'wp_staticize_emoji_for_email');
add_filter( 'emoji_svg_url', create_function( '', 'return false;' ) );//禁用emoji预解析
//去除 XMLRPC, WLW, Generator, Feeds 和 ShortLink
remove_action('wp_head', 'rsd_link'); //removes EditURI/RSD (Really Simple Discovery) link.
remove_action('wp_head', 'wlwmanifest_link'); //removes wlwmanifest (Windows Live Writer) link.
remove_action('wp_head', 'wp_generator'); //removes meta name generator.
remove_action('wp_head', 'wp_shortlink_wp_head'); //removes shortlink.
remove_action( 'wp_head', 'feed_links', 2 ); //removes feed links.
remove_action('wp_head', 'feed_links_extra', 3 );  //removes comments feed.
// 阻止站内文章互相Pingback 
function theme_noself_ping( &$links ) { 
    $home = get_option( 'home' );
    foreach ( $links as $l => $link )
    if ( 0 === strpos( $link, $home ) )
    unset($links[$l]); 
}
add_action('pre_ping','theme_noself_ping');

//禁用REST API功能代码
add_filter('rest_enabled', '_return_false');
add_filter('rest_jsonp_enabled', '_return_false');
//移除wp-json链接
remove_action( 'wp_head', 'rest_output_link_wp_head', 10 );
remove_action( 'wp_head', 'wp_oembed_add_discovery_links', 10 );
//禁用embeds功能
function disable_embeds_init() {
    /* @var WP $wp */
    global $wp;
    $wp->public_query_vars = array_diff( $wp->public_query_vars, array(
        'embed',
    ) );
    remove_action( 'rest_api_init', 'wp_oembed_register_route' );
    add_filter( 'embed_oembed_discover', '__return_false' );
    remove_filter( 'oembed_dataparse', 'wp_filter_oembed_result', 10 );
    remove_action( 'wp_head', 'wp_oembed_add_discovery_links' );
    remove_action( 'wp_head', 'wp_oembed_add_host_js' );
    add_filter( 'tiny_mce_plugins', 'disable_embeds_tiny_mce_plugin' );
    add_filter( 'rewrite_rules_array', 'disable_embeds_rewrites' );
}
add_action( 'init', 'disable_embeds_init', 9999 );
function disable_embeds_tiny_mce_plugin( $plugins ) {
    return array_diff( $plugins, array( 'wpembed' ) );
}
function disable_embeds_rewrites( $rules ) {
    foreach ( $rules as $rule => $rewrite ) {
        if ( false !== strpos( $rewrite, 'embed=true' ) ) {
            unset( $rules[ $rule ] );
        }
    }
    return $rules;
}
function disable_embeds_remove_rewrite_rules() {
    add_filter( 'rewrite_rules_array', 'disable_embeds_rewrites' );
    flush_rewrite_rules();
}
register_activation_hook( __FILE__, 'disable_embeds_remove_rewrite_rules' );
function disable_embeds_flush_rewrite_rules() {
    remove_filter( 'rewrite_rules_array', 'disable_embeds_rewrites' );
    flush_rewrite_rules();
}
register_deactivation_hook( __FILE__, 'disable_embeds_flush_rewrite_rules' );

/*删除左侧菜单,如删除控制台、工具、用户、插件*/
function remove_menus() {
  global $menu;
  $restricted = array(__('Dashboard'), __('Tools'), __('Users'),__('Plugins'));
  end ($menu);
  while (prev($menu)){
    $value = explode(' ',$menu[key($menu)][0]);
    if(strpos($value[0], '<') === FALSE) {
      if(in_array($value[0] != NULL ? $value[0]:"" , $restricted)){
        unset($menu[key($menu)]);
      }
    }
    else {
      $value2 = explode('<', $value[0]);
      if(in_array($value2[0] != NULL ? $value2[0]:"" , $restricted)){
        unset($menu[key($menu)]);
      }
    }
  }
}
if ( is_admin() ) {
  add_action('admin_menu', 'remove_menus');
}

// 同时删除head和feed中的WP版本号
function ludou_remove_wp_version() {
  return '';
}
add_filter('the_generator', 'ludou_remove_wp_version');

// 隐藏js/css附加的WP版本号
function ludou_remove_wp_version_strings( $src ) {
  global $wp_version;
  parse_str(parse_url($src, PHP_URL_QUERY), $query);
  if ( !empty($query['ver']) && $query['ver'] === $wp_version ) {
    // 用WP版本号 + 12.8来替代js/css附加的版本号
    // 既隐藏了WordPress版本号,也不会影响缓存
    // 建议把下面的 12.8 替换成其他数字,以免被别人猜出
    $src = str_replace($wp_version, $wp_version + 12.8, $src);
  }
  return $src;
}
add_filter( 'script_loader_src', 'ludou_remove_wp_version_strings' );
add_filter( 'style_loader_src', 'ludou_remove_wp_version_strings' );

//移除Wordpress后台“感谢使用wordpress创作”和获取wordpress最新版本更新信息
function my_admin_footer_text(){
return '';
}
function my_update_footer()
{
return '';
}
add_filter( 'admin_footer_text', 'my_admin_footer_text', 10 );
add_filter( 'update_footer', 'my_update_footer', 50 );
/* 让 WordPress 禁止自动保存和修订版本 http://www.wpxzt.com*/
function fanly_no_autosave() { wp_deregister_script('autosave'); }
add_filter( 'wp_revisions_to_keep', 'fanly_wp_revisions_to_keep', 10, 2 );
function fanly_wp_revisions_to_keep( $num, $post ) { return 0;}

二、google字体换成国内cdh加速

修改wp-includes/script-loader.php文件

参考链接:

  1. 如何删除WordPress后台左侧不需要的菜单?

添加新评论

已有 7 条评论

拼多多商家专用快递单号,不超时,不降权,单号购买链接www.danhw.com

七牛云加速具体需要怎么做呢

尤其是后台,emmm

然而你页面不允许选中文字……
我不会告诉你头像服务器虽然是cn开头,但也是美国服务器,慢的要死,我刚换为v2ex。

devoted devoted 回复 @后宫学长

可以选中吧。

devoted devoted 回复 @后宫学长

嗯,回头测试下v2ex的。

devoted devoted 回复 @devoted

换成v2ex后,加载gravatar的速度提升3~7倍左右(⊙o⊙)