WordPress可以禁用哪些模块能提高打开加载的速度?

22次阅读
2024年09月13日 09:05:32

共计 3469 个字符,预计需要花费 9 分钟才能阅读完成。

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

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

编辑functions.php 文件,使用代码编辑器修改它。

禁用谷歌字体

如果使用了 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;
}

强制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 功能

/* 禁用 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');

屏蔽若干无用功能

// 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' );
正文完
 0
鹿泽
版权声明:本站原创文章,本文由 鹿泽 于 2024年9月13日 09:05:32 发表,共计 3469 字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
本站所有文章,如无特殊说明或标注,均为本站原创发布。任何在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。
如若本站内容侵犯了原著者的合法权益,可联系我们邮箱:526009505@qq.com进行处理。
最新文章
网站优化seo推广服务如何做好内链?

网站优化seo推广服务如何做好内链?

在一个网站刚刚建设的初期,网站内链的建设工作尤为重要,可以说网站内链做得好SEO就成功了一半。今天就来讲讲网站...
WordPress博客主题二次元风-lolimeow主题更新10.1版,重构样式

WordPress博客主题二次元风-lolimeow主题更新10.1版,重构样式

一个WordPress博客主题二次元风主题,本站之前启用过,后面更换了,不过很好看,适合大家选择使用: 202...
专业优化公司seo是怎么做好404页面的?

专业优化公司seo是怎么做好404页面的?

404页面的目的是:告诉浏览者其所请求的页面不存在或链接错误,同时引导用户使用网站其它页面而不是关闭窗口离开。...
网站搜索优化seo如何做好内部链接优化?

网站搜索优化seo如何做好内部链接优化?

网站内部链接的布局很多做SEO的朋友都知道,非常重要。网站想做的越大,就要更加重视内部链接的布局。因为良好的内...
影响关键词优化根本的排序因素

影响关键词优化根本的排序因素

对于像百度搜索来说,并没有排序这一说法,搜索引擎认为排序是在特定的关键词下网站内容的位置,而关键词是由用户搜索...