wordpress需要做的优化
wodpress安装好后,为了适应国内网络环境,有些东西需要调整或是移除,可以有效的提高访问速度,主要有以方面:
1、替换头像网址,防止官方网址无法访问造成打开网页缓慢:
if (!function_exists('replace_to_v2ex_avatar')) {
function replace_to_v2ex_avatar($avatarUrl) {
return preg_replace(["/[0-9]\.gravatar\.com\/avatar/", "/secure.gravatar\.com\/avatar/"], "cdn.v2ex.com/gravatar", $avatarUrl);
}
}
add_filter('get_avatar', 'replace_to_v2ex_avatar');
2、移除谷歌字体,同样是为了防止官方网址打开缓慢,直接移除:
if (!function_exists('remove_wp_open_sans')) {
function remove_wp_open_sans() {
wp_deregister_style('open-sans');
wp_register_style('open-sans', false);
}
}
// 前台删除Google字体CSS
add_filter('wp_enqueue_scripts', 'remove_wp_open_sans');
// 后台删除Google字体CSS
add_filter('admin_enqueue_scripts', 'remove_wp_open_sans');
3、替换默认的jquery,使用CDN上的jquery,提高加载速度:
if (!function_exists('register_my_jquery')) {
function register_my_jquery() {
if (!is_admin()) {
wp_deregister_script('jquery-core');
wp_register_script('jquery-core', '//cdn.bootcss.com/jquery/3.1.1/jquery.min.js', true, '3.1.1');
wp_enqueue_script('jquery-core');
wp_deregister_script('jquery-migrate');
wp_register_script('jquery-migrate', '//cdn.bootcss.com/jquery-migrate/3.0.0/jquery-migrate.min.js', true, '3.0.0');
wp_enqueue_script('jquery-migrate');
}
}
}
add_action('wp_enqueue_scripts', 'register_my_jquery');
4、禁用emoji,这个真的是作用不大,感觉多余,直接移除:
function disable_emojis() {
remove_action( 'wp_head', 'print_emoji_detection_script', 7 );
remove_action( 'admin_print_scripts', 'print_emoji_detection_script' );
remove_action( 'wp_print_styles', 'print_emoji_styles' );
remove_action( 'admin_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( 'tiny_mce_plugins', 'disable_emojis_tinymce' );
}
add_action( 'init', 'disable_emojis' );
//Filter function used to remove the tinymce emoji plugin.
function disable_emojis_tinymce( $plugins ) {
if ( is_array( $plugins ) ) {
return array_diff( $plugins, array( 'wpemoji' ) );
} else {
return array();
}
}
5、禁用embeds,这个也没什么作用,虽然说只是移除wp-embed.min.js,但代码稍微有点多,直接应用没事:
function disable_embeds_init() {
global $wp;
// Remove the embed query var.
$wp->public_query_vars = array_diff( $wp->public_query_vars, array('embed',));
// Remove the REST API endpoint.
remove_action( 'rest_api_init', 'wp_oembed_register_route' );
// Turn off
add_filter( 'embed_oembed_discover', '__return_false' );
// Don't filter oEmbed results.
remove_filter( 'oembed_dataparse', 'wp_filter_oembed_result', 10 );
// Remove oEmbed discovery links.
remove_action( 'wp_head', 'wp_oembed_add_discovery_links' );
// Remove oEmbed-specific JavaScript from the front-end and back-end.
remove_action( 'wp_head', 'wp_oembed_add_host_js' );
add_filter( 'tiny_mce_plugins', 'disable_embeds_tiny_mce_plugin' );
// Remove all embeds rewrite rules.
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' );
如果对头部的一些代码感觉多余,也可以移除使用下面的代码即可:
//移除头部不必要的链接
remove_action( 'wp_head', 'wp_resource_hints', 2);// 去除DNS预解析
remove_action( 'wp_head', 'wp_generator' ); //隐藏wordpress版本
remove_action( 'wp_head', 'rsd_link' );//移除头部RSD链接
remove_action( 'wp_head', 'wlwmanifest_link' );//移除头部wlwmanifest链接
remove_action( 'wp_head', 'rest_output_link_wp_head', 10 );//移除头部wp-json链接
remove_action( 'wp_head', 'wp_shortlink_wp_head'); //移除头部shortlink
remove_action( 'wp_head', 'rel_canonical' );//移除canonical标记
remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head');//移除头部上下篇链接
remove_action( 'wp_head', 'feed_links_extra', 3);// 移除额外的feed链接
remove_action( 'wp_head', 'wp_oembed_add_host_js' );//移除wp-embed.min.js
remove_action( 'wp_head', 'wp_oembed_add_discovery_links', 10 );//移除头部oembed链接
add_filter( 'show_admin_bar', '__return_false' ); //删除AdminBar
add_filter('xmlrpc_enabled', '__return_false');//屏蔽 XML-RPC (pingback) 的功能