说明:WordPress由本国人开拓的,使用了许多海外站点服务,譬如Gravatar镜像、谷歌字体之类的,因为咱们在大陆,链接速率自然就慢了许多,有的还时时时的被墙,很影响使用,同时功能很超强,只是许多咱们都不需要,这里咱们可以通过修正function.php来精简WordPress,从而使站点速率变快。
注意:WordPress加快的一个要点就是能不用插件就不要用插件,插件越多站点越慢。
方式
function.php文件普通在正在使用的正题根目录。
1、禁用谷歌字体
如果使用了WordPress原始的正题那么需要通过插件处理: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;
}
2、掉换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');
3、胁制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' );
4、去除加载的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' );
5、清除WP头不需要的代码
这个免去了,可以有效精简WordPress富余的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');
6、禁用Emoji功能
WordPress的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');
7、屏蔽文章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');
8、关闭XML-RPC功能
// 关闭 XML-RPC 功能
add_filter('xmlrpc_enabled', '__return_false');
9、屏蔽REST API
// 屏蔽 REST API
add_filter('rest_enabled', '__return_false');
add_filter('rest_jsonp_enabled', '__return_false');
10、移除头部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 );
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' );
这些都完成后,咱们会发现站点显著的变快了!
希望以上的文章对各位有用,如果觉得不错给我点个喜欢吧!更多和通过修改function文件来使WordPress网站加载速度更快相关的问题或者对德国主机租用有疑惑也欢迎大家咨询。
网友评论