WordPress 网站速度优化的16个技巧,提速100%

你现在是否被WordPress主题缓慢的加载速度而差点劝退?如果你正在忍受,不妨跟我学几个简单的WordPress主题提速小技巧,复制粘贴即可提升网站100%的速度!为你节省近2000元的网站速度优化费用。

准备开始第一步

优化步骤:
登录后台 – [ 外观 ] – [主题编辑器] 找到 functions.php,

复制你需要的功能到文件内保存即可,如下图

在这里插入图片描述

一.关闭emoji表情

/**
 * 关闭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');

function disable_emojis_tinymce($plugins){
    if (is_array($plugins)) {
        return array_diff($plugins, array(
            'wpemoji'
        ));
    } else {
        return array();
    }
}

二、WordPress 自动更新和后台更新检查\

/**
 * 彻底关闭 WordPress 自动更新和后台更新检查
 */
// 彻底关闭自动更新
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' );

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 更新主题

三、去除WordPress头部冗余代码

/**
 * 去除WordPress头部冗余代码
 */
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_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);
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'
    ));
}
add_action('widgets_init', 'my_remove_recent_comments_style');

四、禁用后台自动保存

/*
 * 禁用后台自动保存
 */
add_action('admin_print_scripts', create_function('$a', "wp_deregister_script('autosave');"));

五、关闭文章修订版本

/**
 * 关闭文章修订版本
 */
add_filter('wp_revisions_to_keep', 'specs_wp_revisions_to_keep', 10, 2);
function specs_wp_revisions_to_keep($num, $post){
    return 0;
}

六、去除后台首页面板的功能

/**
 * 去除后台首页面板的功能
 */
add_action('wp_dashboard_setup', 'cqr_remove_dashboard_widgets');
function cqr_remove_dashboard_widgets(){
    global $wp_meta_boxes;
    unset($wp_meta_boxes['dashboard']['normal']);
    unset($wp_meta_boxes['dashboard']['side']);
}

七、移除 WordPress 头部加载 DNS 预获取(dns-prefetch)

/**
 * 移除 WordPress 头部加载 DNS 预获取(dns-prefetch)
 */
function remove_dns_prefetch( $hints, $relation_type ) {
    if ( 'dns-prefetch' === $relation_type ) {
        return array_diff( wp_dependencies_unique_hosts(), $hints );
    }
 
    return $hints;
}
add_filter( 'wp_resource_hints', 'remove_dns_prefetch', 10, 2 );

八、删除不常用的自定义仪表板

/**
 * 自定义仪表板
 * 删除这些Dashboard构件
 */

function remove_dashboard_widgets(){
    global $wp_meta_boxes;
    unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_quick_press']);
    unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_incoming_links']);
    unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_right_now']);
    unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_plugins']);
    unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_recent_drafts']);
    unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_recent_comments']);
    unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_primary']);
    unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_secondary']);
}
add_action('wp_dashboard_setup', 'remove_dashboard_widgets', 11);

九、缓存媒体文件获取的月份

/**
 * 缓存媒体文件获取的月份
 */
add_filter('media_library_months_with_files', function($months){
	$months	= get_transient('doocii_media_library_months');

	if($months === false) {
		global $wpdb;

		$months = $wpdb->get_results("SELECT DISTINCT YEAR( post_date ) AS year, MONTH( post_date ) AS month FROM $wpdb->posts WHERE post_type = 'attachment' ORDER BY post_date DESC");

		set_transient('doocii_media_library_months', $months, WEEK_IN_SECONDS);
	}

	return $months;
});

// 删除附件月份的缓存
function doocii_delete_media_library_months_cache(){
	delete_transient('doocii_media_library_months');
}
add_action('edit_attachment',	'doocii_delete_media_library_months_cache');
add_action('add_attachment',	'doocii_delete_media_library_months_cache');
add_action('delete_attachment',	'doocii_delete_media_library_months_cache');

十、屏蔽文章 Embed 功能

/**
 * 屏蔽文章 Embed 功能,添加带embed或视频链接到编辑器中,转不会被转换。
 */
remove_action('rest_api_init', 'wp_oembed_register_route');
remove_filter('rest_pre_serve_request', '_oembed_rest_pre_serve_request', 10, 4);
add_filter('embed_oembed_discover', '__return_false');
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');
add_filter('tiny_mce_plugins', 'cqr_disable_post_embed_tiny_mce_plugin');
function cqr_disable_post_embed_tiny_mce_plugin($plugins){
    return array_diff($plugins, array('wpembed'));
}
add_filter('query_vars', 'cqr_disable_post_embed_query_var');
function cqr_disable_post_embed_query_var($public_query_vars){
    return array_diff($public_query_vars, array('embed'));
}

十一、禁用 Auto Embeds 功能

Auto Embeds 基本不支持国内网站,禁用,加快页面解析速度。

/**
 * 禁用 Auto Embeds 功能,Auto Embeds 基本不支持国内网站,禁用,加快页面解析速度。
 */
remove_filter('the_content', array($GLOBALS['wp_embed'], 'run_shortcode'), 8);
remove_filter('the_content', array($GLOBALS['wp_embed'], 'autoembed'), 8);
remove_action('pre_post_update', array($GLOBALS['wp_embed'], 'delete_oembed_caches'));
remove_action('edit_form_advanced', array($GLOBALS['wp_embed'], 'maybe_run_ajax_cache'));

十二、禁用 WooCommerce 块的 CSS 样式

/**
 * 禁用 WooCommerce 块的 CSS 样式
 */
function themesharbor_disable_woocommerce_block_styles() {
  wp_dequeue_style( 'wc-blocks-style' );
}
add_action( 'wp_enqueue_scripts', 'themesharbor_disable_woocommerce_block_styles' );

十三、禁用 WordPress 4.4+ 的响应式图片功能

/**
 * 禁用 WordPress 4.4+ 的响应式图片功能
 */
    add_filter('max_srcset_image_width', create_function('', 'return 1;'));
    ```
## 十五、删除WordPress核心音频/视频播放器
```php
/**
 * 删除WordPress核心音频/视频播放器
 */
wp_deregister_script('wp-mediaelement');
wp_deregister_style('wp-mediaelement');

十六、关闭讨论显示头像功能

方法一:网站后台– [讨论] -[头像]-[显示头像],取消勾选即可,国内网站提速明显。

方法二(替换默认头像):

define('DEFAULT_AVATAR_URL', '//xxx.xxx.com/imgs/2021/12/ce6cd74ae0c1d3e3.jpeg');

function no_gravatars( $avatar ) {
    return preg_replace( "/http.*?gravatar\.com[^\']*/", DEFAULT_AVATAR_URL, $avatar );
}
add_filter( 'get_avatar', 'no_gravatars' );
  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值