• 欢迎访问db1024网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏db1024吧

通过修改function文件来使WordPress网站加载速度更快

主机教程 Liam 1年前 (2019-05-03) 664次浏览 27个评论 扫描二维码
文章目录[隐藏]

说明: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
使用V2EXGravatar镜像来代替原来的,支持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功能
WordPressEMOJI图片国内无法使用,直接禁用。

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

这些都完成后,我们会发现网站明显的变快了!


db1024 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:通过修改function文件来使WordPress网站加载速度更快
喜欢 (1)
关于作者:
发表我的评论
取消评论
表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
(27)个小伙伴在吐槽
  1. After looking over a few of the blog articles on your site, I truly like your technique of blogging. I book marked it to my bookmark webpage list and will be checking back soon. Please check out my web site as well and tell me your opinion.
    webhosting2020-08-31 23:44 回复 Mac OS X | Chrome 67.0.3396.87
  2. The other day, while I was at work, my sister stole my iPad and tested to see if it can survive a twenty five foot drop, just so she can be a youtube sensation. My apple ipad is now broken and she has 83 views. I know this is totally off topic but I had to share it with someone!
    best web hosting2020-08-31 22:29 回复 Windows 8.1 | Firefox浏览器 60.0
  3. It is perfect time to make some plans for the future and it is time to be happy. I have read this post and if I could I wish to suggest you few interesting things or tips. Maybe you could write next articles referring to this article. I want to read even more things about it!
    black mass2020-08-31 10:42 回复 Windows 7 | Internet Explorer 9.0
  4. Wonderful website you have here but I was curious about if you knew of any discussion boards that cover the same topics talked about in this article? I'd really like to be a part of community where I can get opinions from other knowledgeable individuals that share the same interest. If you have any recommendations, please let me know. Cheers!
    black mass2020-08-28 20:35 回复 Windows 7 | Internet Explorer 9.0
  5. Thanks for every other informative blog. Where else could I get that type of information written in such a perfect means? I've a challenge that I am just now working on, and I have been on the glance out for such information.
    cheap flights2020-08-28 04:33 回复 Linux | Firefox浏览器 52.9
  6. Aw, this was an incredibly nice post. Taking the time and actual effort to produce a great article… but what can I say… I hesitate a whole lot and don't manage to get nearly anything done. y2yxvvfw cheap flights
    cheap flights2020-08-26 05:22 回复 Mac OS X | Safari浏览器 605.1.15
  7. Superb post however , I was wanting to know if you could write a litte more on this subject? I'd be very grateful if you could elaborate a little bit further. Many thanks! 34pIoq5 cheap flights
    cheap flights2020-08-25 20:33 回复 Mac OS X | Chrome 56.0.2986.42
  8. With havin so much content do you ever run into any problems of plagorism or copyright violation? My blog has a lot of exclusive content I've either created myself or outsourced but it appears a lot of it is popping it up all over the web without my authorization. Do you know any ways to help stop content from being ripped off? I'd genuinely appreciate it. cheap flights 32hvAj4
    cheap flights2020-08-25 04:20 回复 Mac OS X | Chrome 65.0.3325.181
  9. Hello friends, its great post on the topic of tutoringand entirely defined, keep it up all the time.
    cheap flights2020-08-24 12:55 回复 Linux | Chrome 66.0.3359.117
  10. Post writing is also a fun, if you be familiar with afterward you can write or else it is complicated to write.
    best website hosting2020-08-14 11:47 回复 Mac OS X | Firefox浏览器 59.0
  11. Awesome! Its in fact remarkable post, I have got much clear idea regarding from this post.
    web hosting company2020-08-11 20:17 回复 Windows 8.1 | Firefox浏览器 60.0
  12. You are so awesome! I do not suppose I've read through something like that before. So nice to discover somebody with a few genuine thoughts on this issue. Seriously.. thank you for starting this up. This site is one thing that is required on the web, someone with a bit of originality!
    web hosting providers2020-08-07 20:24 回复 Linux | Chrome 61.0.3163.128
  13. Hi there friends, nice paragraph and good urging commented at this place, I am really enjoying by these.
    website hosting companies2020-08-05 19:49 回复 Linux | Chrome 65.0.3325.181
  14. It's really a cool and helpful piece of info. I'm happy that you just shared this useful info with us. Please stay us informed like this. Thank you for sharing.
    cheap flights to alicante2020-07-31 12:15 回复 Linux | Chrome 64.0.3282.167
  15. I will right away clutch your rss as I can not in finding your e-mail subscription hyperlink or newsletter service. Do you have any? Kindly allow me know in order that I may just subscribe. Thanks. adreamoftrains web hosting reviews
    adreamoftrains web hosting services2020-07-30 20:39 回复 Mac OS X | Chrome 67.0.3396.79
  16. Hello i am kavin, its my first time to commenting anywhere, when i read this post i thought i could also create comment due to this sensible post.
    cheap flights to alicante2020-07-27 22:16 回复 Linux | 未知浏览器
  17. I do believe all of the concepts you've introduced on your post. They're really convincing and can certainly work. Still, the posts are very brief for starters. May just you please lengthen them a little from subsequent time? Thank you for the post.
    web hosting companies2020-07-27 03:31 回复 Mac OS X | Firefox浏览器 56.0.4
  18. Good post. I learn something totally new and challenging on sites I stumbleupon every day. It will always be interesting to read content from other writers and practice a little something from other sites.
    hosting services2020-07-26 09:31 回复 Linux | Chrome 63.0.3239.132
  19. Nice post. I learn something totally new and challenging on websites I stumbleupon on a daily basis. It will always be exciting to read content from other writers and use a little something from their websites.
    web hosting providers2020-07-23 11:54 回复 Linux | Chrome 67.0.3394.0
  20. I am sure this paragraph has touched all the internet viewers, its really really nice paragraph on building up new web site.
    web hosting providers2020-07-22 08:59 回复 Linux | Firefox浏览器 2.0.0.6
  21. Its such as you read my thoughts! You appear to know a lot approximately this, such as you wrote the guide in it or something. I believe that you could do with a few % to force the message house a bit, however other than that, this is great blog. An excellent read. I'll certainly be back.
    best web hosting2020-07-16 18:30 回复 Linux | Chrome 66.0.3359.117
  22. Appreciate the recommendation. Let me try it out.
    cbd oil that works 20202020-06-29 17:57 回复 Linux | Chrome 66.0.3359.117
  23. I am in fact glad to read this blog posts which contains tons of valuable information, thanks for providing such information.
    cbd oil that works 20202020-06-28 11:58 回复 Linux | Chrome 51.0.2705.45
  24. Hello, yeah this piece of writing is genuinely nice and I have learned lot of things from it on the topic of blogging. thanks.
    cbd oil that works 20202020-06-27 23:38 回复 Windows 8.1 | Chrome 66.0.3359.139
  25. Since the admin of this website is working, no hesitation very shortly it will be famous, due to its quality contents.
    tinyurl.com2020-06-27 13:12 回复 Windows 8.1 | Firefox浏览器 60.0
  26. First off I want to say excellent blog! I had a quick question that I'd like to ask if you do not mind. I was curious to know how you center yourself and clear your mind prior to writing. I have had difficulty clearing my thoughts in getting my thoughts out there. I do enjoy writing however it just seems like the first 10 to 15 minutes are usually wasted just trying to figure out how to begin. Any suggestions or tips? Thank you!
    tinyurl.com2020-06-26 20:51 回复 Windows 8.1 | Chrome 67.0.3381.1
  27. This post offers clear idea in favor of the new visitors of blogging, that really how to do running a blog.
    g rsacwgxy2020-06-22 13:18 回复 Linux | Chrome 63.0.3239.132