首先在我在最开始使用wordpress就经历了很久的速度慢的困扰,经过这么久的使用。把心得分享出来。网站的打开速度的重要性我就不讲了。大家都知道。
一,如何查看网站的访问速度
有很多人会说我网站速度还可以啊,那是你经常在自己的电脑上浏览网站,有缓存的存在。最好用工具来测一下。测试网站访问速度的工具很多,给大家推荐我常用的:
1.国内百度优化:站长工具测速。
2.国外谷歌优化就用: Pingdom ,pingdom是一个免费的工具,可以测试网站在不同地区的速度。
二,用插件提升wordpress网站速度
众所周知,wordpress的有点就是插件多,很多功能用插件几乎都可以解决。主要解决两个问题
1.缓存问题(参考wordpress常用插件)
2.js.css问题( 参考wordpress常用插件 )
这两个问题都可以使用插件解决。
三,代码实现wordpress访问速度
在functions.php加入下面的代码即可,代码后面的注释,可根据自己的需要加入:
//洗header remove_action( 'wp_head', 'feed_links', 2 ); //去除文章feed remove_action( 'wp_head', 'rsd_link' ); //针对Blog的远程离线编辑器接口 remove_action( 'wp_head', 'wlwmanifest_link' ); //Windows Live Writer接口 remove_action( 'wp_head', 'parent_post_rel_link', 10, 0 ); //移除后面文章的url remove_action( 'wp_head', 'start_post_rel_link', 10, 0 ); //移除最开始文章的url remove_action( 'wp_head', 'wp_shortlink_wp_head', 10, 0 );//自动生成的短链接 remove_action( 'wp_head', 'wp_generator' ); // 移除版本号 remove_action('wp_head', 'index_rel_link');//当前文章的索引 remove_action('wp_head', 'feed_links_extra', 3);// 额外的feed,例如category, tag页 remove_action('wp_head', 'adjacent_posts_rel_link', 10, 0); // 上、下篇. remove_action('wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0 );//rel=pre wp_deregister_script('l10n'); remove_filter('the_content', 'wptexturize');//禁用半角符号自动转换为全角 remove_action('wp_head', 'wp_resource_hints', 2);//禁用类似rel='dns-prefetch' href='//fonts.googleapis.com' //移除wp-json add_filter('json_enabled', '__return_false' ); add_filter('json_jsonp_enabled', '__return_false' ); remove_action( 'wp_head', 'rest_output_link_wp_head', 10 ); remove_action( 'wp_head', 'wp_oembed_add_discovery_links', 10 ); /** * 移除 WordPress 加载的JS和CSS链接中的版本号 * https://www.wpdaxue.com/remove-js-css-version.html */ function wpdaxue_remove_cssjs_ver( $src ) { if( strpos( $src, 'ver=' ) ) $src = remove_query_arg( 'ver', $src ); return $src; } add_filter( 'style_loader_src', 'wpdaxue_remove_cssjs_ver', 999 ); add_filter( 'script_loader_src', 'wpdaxue_remove_cssjs_ver', 999 );
2.设置静态内容的缓存,加快速度,其实这个很简单,直接加入以下代码就可以了。在.htaccess中。
<IfModule mod_expires.c>
ExpiresActive on
# Perhaps better to whitelist expires rules? Perhaps.
ExpiresDefault “access plus 1 month”
# cache.appcache needs re-requests
# in FF 3.6 (thx Remy ~Introducing HTML5)
ExpiresByType text/cache-manifest “access plus 0 seconds”
# Your document html
ExpiresByType text/html “access plus 0 seconds”
# Data
ExpiresByType text/xml “access plus 0 seconds”
ExpiresByType application/xml “access plus 0 seconds”
ExpiresByType application/json “access plus 0 seconds”
# RSS feed
ExpiresByType application/rss+xml “access plus 1 hour”
# Favicon (cannot be renamed)
ExpiresByType image/x-icon “access plus 1 week”
# Media: images, video, audio
ExpiresByType image/gif “access plus 1 month”
ExpiresByType image/png “access plus 1 month”
ExpiresByType image/jpg “access plus 1 month”
ExpiresByType image/jpeg “access plus 1 month”
ExpiresByType video/ogg “access plus 1 month”
ExpiresByType audio/ogg “access plus 1 month”
ExpiresByType video/mp4 “access plus 1 month”
ExpiresByType video/webm “access plus 1 month”
# HTC files (css3pie)
ExpiresByType text/x-component “access plus 1 month”
# Webfonts
ExpiresByType font/truetype “access plus 1 month”
ExpiresByType font/opentype “access plus 1 month”
ExpiresByType application/x-font-woff “access plus 1 month”
ExpiresByType image/svg+xml “access plus 1 month”
ExpiresByType application/vnd.ms-fontobject “access plus 1 month”
# CSS and JavaScript
ExpiresByType text/css “access plus 1 year”
ExpiresByType application/javascript “access plus 1 year”
ExpiresByType text/javascript “access plus 1 year”
<IfModule mod_headers.c>
Header append Cache-Control “public”
</IfModule>
</IfModule>
原创文章,作者:yuyi,如若转载,请注明出处:https://www.seoyzy.com/181.htm