米饭粑 米饭粑
  • 首页
  • 好物推荐
    • 干货分享
    • 好物推荐
    • 免费资源
    • 非专业测评
  • 技术架构
    • Linux
    • HTTPS
    • Windows Server
  • 关于米饭
  • 教程&更多
    • 更多教程
    • 资讯新闻
    • 琐琐碎碎
    • 打赏米饭
    • 阿里云Ping
  • 友情链接
首页 › 技术架构 › 教程 › 让 WordPress 飞起来的几个 function

让 WordPress 飞起来的几个 function

妙正灰
5年前教程阅读 2,886

介绍

WordPress 毕竟是歪果仁开发的系统放到中国来会有一些水土不服,因为有广泛的使用到一些境外网站服务以及一些不必要的功能导致 WP 网站变慢,这里就将介绍如何通过 function.php 来精简 WP。

functions.php 是 WordPress 预留的功能函数文件,专门用于添加各种自定义函数代码。现在的WordPress主题的功能越来越强大了,一般都自带了 functions.php 这个文件(通常在主题根目录)。

WordPress 加速的一个要点就是能不用插件就不要用插件,插件越多网站越慢 。

例如,我的主题目录是:/data/wwwroot/www.mf8.biz/wp-content/themes/mf8themes/,那么在该目录下就存在 functions.php 文件,使用代码编辑器修改它。

内容

首先,我们将那些被墙的内容去掉,打不开会拖慢速度。下面是我从各网站搜罗来的 WP 精简 function 内容,大家选择性使用,尽量一个一个的添加,因为有些主题和插件可能会依赖到某些函数。

禁用谷歌字体

如果使用了 WP 默认的主题那么需要通过插件解决: Remove Open Sans font Link from WP core

如果是其他主题,添加:

/**
 * WordPress 后台禁用Google Open Sans字体,加速网站
 */
add_filter( 'gettext_with_context', 'wpdx_disable_open_sans', 888, 4 );
function wpdx_disable_open_sans( $translations, $text, $context, $domain ) {
  if ( 'Open Sans font: on or off' == $context && 'on' == $text ) {
    $translations = 'off';
  }
  return $translations;
}

替换 Gravatar

使用 V2EX 的 Gravatar 镜像来代替原来的,支持 HTTPS。

function get_ssl_avatar($avatar) {
   $avatar = preg_replace('/.*\/avatar\/(.*)\?s=([\d]+)&.*/','<img src="https://cdn.v2ex.co/gravatar/$1?s=$2" class="avatar avatar-$2" height="$2" width="$2">',$avatar);
   return $avatar;
}
add_filter('get_avatar', 'get_ssl_avatar');

强制jquery库文件底部载入

将 JS 放到最后加载,有利于提高网站加载效率

//强制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' );

去除加载的css和js后面的版本号

去掉版本查询提高效率,看着也舒心

//去除加载的css和js后面的版本号
/** Remove Query strings from Static Resources. */
function _remove_script_version( $src ){
    $parts = explode( '?', $src );
    return $parts[0];
}
add_filter( 'script_loader_src', '_remove_script_version', 15, 1 );
add_filter( 'style_loader_src', '_remove_script_version', 15, 1 );
add_filter( 'pre_option_link_manager_enabled', '__return_true' );

删除 WP 头不需要的代码

这个去掉了,可以有效精简 WP 多余的nearing

//删除 wp_head 中无关紧要的代码
remove_action('wp_head', 'rsd_link');
remove_action('wp_head', 'wlwmanifest_link');
remove_action('wp_head', 'wp_generator');
remove_action('wp_head', 'start_post_rel_link');
remove_action('wp_head', 'index_rel_link');
remove_action('wp_head', 'adjacent_posts_rel_link');

禁用 Emoji 功能

WP 的 EMOJI 图片国内无法使用,直接禁用

/* 禁用 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_action('embed_head',     'print_emoji_detection_script');

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');

屏蔽文章 Embed 功能

多余功能,去掉

remove_action('rest_api_init', 'wp_oembed_register_route');
remove_filter('rest_pre_serve_request', '_oembed_rest_pre_serve_request', 10, 4);

remove_filter('oembed_dataparse', 'wp_filter_oembed_result', 10 );
remove_filter('oembed_response_data',   'get_oembed_response_data_rich',  10, 4);

remove_action('wp_head', 'wp_oembed_add_discovery_links');
remove_action('wp_head', 'wp_oembed_add_host_js');

关闭 XML-RPC 功能

// 关闭 XML-RPC 功能 
 add_filter('xmlrpc_enabled', '__return_false');

屏蔽 REST API

// 屏蔽 REST API
add_filter('rest_enabled', '__return_false');
add_filter('rest_jsonp_enabled', '__return_false');

移除头部 wp-json 标签和 HTTP header 中的 link

//移除头部 wp-json 标签和 HTTP header 中的 link 
remove_action('wp_head', 'rest_output_link_wp_head', 10 );
remove_action('template_redirect', 'rest_output_link_header', 11 );

屏蔽若干无用功能

// Disable auto-embeds for WordPress >= v3.5
remove_filter( 'the_content', array( $GLOBALS['wp_embed'], 'autoembed' ), 8 );


add_filter('automatic_updater_disabled', '__return_true');  // 彻底关闭自动更新

remove_action('init', 'wp_schedule_update_checks'); // 关闭更新检查定时作业
wp_clear_scheduled_hook('wp_version_check');            // 移除已有的版本检查定时作业
wp_clear_scheduled_hook('wp_update_plugins');       // 移除已有的插件更新定时作业
wp_clear_scheduled_hook('wp_update_themes');            // 移除已有的主题更新定时作业
wp_clear_scheduled_hook('wp_maybe_auto_update');        // 移除已有的自动更新定时作业

remove_action( 'admin_init', '_maybe_update_core' );        // 移除后台内核更新检查

remove_action( 'load-plugins.php', 'wp_update_plugins' );   // 移除后台插件更新检查
remove_action( 'load-update.php', 'wp_update_plugins' );
remove_action( 'load-update-core.php', 'wp_update_plugins' );
remove_action( 'admin_init', '_maybe_update_plugins' );

remove_action( 'load-themes.php', 'wp_update_themes' );     // 移除后台主题更新检查
remove_action( 'load-update.php', 'wp_update_themes' );
remove_action( 'load-update-core.php', 'wp_update_themes' );
remove_action( 'admin_init', '_maybe_update_themes' );
Wordpress
赞(1)
Ubuntu 下轻松实现 PHP 多版本共存
上一篇
使用邮件推送服务设置 WP SMTP 发件
下一篇

评论已关闭

标签
Appnode Brotli CDN CentOS CentOS8 Debian DNS ECC ECS Ghost HTTP2 HTTPS IIS Let's Encrypt Linux LiteSpeed MariaDB MySQL Nginx OLS OpenLiteSpeed OpenResty OSS PageSpeed Percona PHP PHP7 Redis RHEL RHEL8 SSL TokuDB Ubuntu WIndows Windows Server 2016 Wordpress 云服务器 升级 域名 微软 数据库 百度 笔记本 腾讯云 阿里云
归档
4月4日,全国哀悼,简单CSS代码将网站变成灰色
2年前
999 0
使用如下姿势预防阿里云 CDN 产生天价账单
2年前
1,156 2
PHP7.4 新特性 预缓存(Preload )介绍 & WordPress 开启 Preload
2年前
1,447 1
站在巨人WordPress的肩膀上学架构
2年前
918 5
1
  • 1
Copyright © 2011-2022 米饭粑. Designed by nicetheme.
浙ICP备15006212号-1
  • 首页
  • 教程
  • 好物
  • 关于
  • 链接
  • 打赏
# 402 # # 113 # # 546 # # 548 # # 460 #
妙正灰
文科屌丝伪IT男一枚.
337
文章
385
评论
454
喜欢