WordPress网站速度优化技巧(珍藏代码版)让您的网站速度提升100% - 汇站网

WordPress网站速度优化技巧(珍藏代码版)让您的网站速度提升100%

2023-09-27 0 440

前言:

你是否对 WordPress 主题的加载速度感到不满意,甚至考虑放弃使用?如果你正在忍受这个问题,不妨学习一些简单的 WordPress 主题加速技巧,只需复制粘贴即可提升网站速度 100%!这样你就能省下将近 2000 元的网站速度优化费用。

准备开始第一步

优化步骤:

登录后台 – [ 外观 ] – [主题编辑器] 找到 functions.php
也可以自行登录服务器到网站主题目录,一般路径是/wp-content/themes/主题名称,打开 functions.php 文件进行编辑。
复制你需要的功能到文件内保存即可,如下图:
WordPress网站速度优化技巧(珍藏代码版)让您的网站速度提升100%

一.关闭 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' );
 

结语:

最好刷新一下自己的网站前端和登录后台,是不是如飞的一样轻快!

ps:

汇站提供的 wordpress 加速优化代码,有些主题不兼容,如果出现不兼容,大家自行删除不兼容的代码便是。

转载请注明:汇站网 » WordPress 网站速度优化技巧(珍藏代码版)让您的网站速度提升 100%

收藏 (0)

微信扫一扫

支付宝扫一扫

点赞 (0)

感谢您的来访,获取更多精彩资源请收藏本站。

本站声明

本资源仅用于个人学习和研究使用,禁止用于任何商业环境!

 1.  本网站名称:汇站网
 2.  本站永久网址:https://www.huizhanii.com/
 3.  本站所有资源来源于网友投稿和高价购买,所有资源仅对编程人员及源代码爱好者开放下载做参考和研究及学习,本站不提供任何技术服务!
 4.  未经原版权作者许可,禁止用于任何商业环境,任何人不得擅作它用,下载者不得用于违反国家法律,否则发生的一切法律后果自行承担!
 5.  为尊重作者版权,请在下载24小时内删除!请购买原版授权作品,支持你喜欢的作者,谢谢!
 6.  若资源侵犯了您的合法权益, 请持您的版权证书和相关原作品信息来信通知我们请来信     通知我们我们会及时删除,给您带来的不便,我们深表歉意!
 7.  如下载链接失效、广告或者压缩包问题请联系站长处理!
 8.  如果你也有好源码或者教程,可以发布到网站,分享有金币奖励和额外收入!
 9.  本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
 10.  因源码具有可复制性,一经赞助 ,不得以任何形式退款。
 11.  更多详情请点击查看

汇站网 WordPress教程 WordPress网站速度优化技巧(珍藏代码版)让您的网站速度提升100% https://www.huizhanii.com/33287.html

汇站

站长资源下载中心-找源码上汇站

常见问题
  • 如果付款后没有弹出下载页面,多刷新几下,有问题联系客服!
查看详情
  • 本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。
查看详情

相关文章

发表评论
暂无评论
  随机评论 表情开关按钮图片
表情表情表情表情表情表情表情表情表情表情表情表情表情表情表情表情表情表情表情表情表情表情表情表情表情表情表情表情表情表情表情表情表情表情表情表情表情表情表情
登录后评论
联系官方客服

为您解决烦忧 - 24小时在线 专业服务

(汇站网)一个专注站长资源的平台网站,提供最新的网站模板和整站源码,内容包含各类精品网页模板,企业网站模板,网站模板,DIV+CSS模板,织梦模板,帝国cms模板,discuz模板,wordpress模板,个人博客论坛模板,上千种免费网页模板下载尽在汇站网.找源码上汇站.huizhanii.com