我们很多网友,尤其是很多WordPress新手会发现自己在使用这款程序建站的时候为什么网站打开速度很慢,而有些网友即便数据和图片比较多网站打开速度也是比较快的,甚至你们都是使用的同一家的服务器。这个就在于我们在使用WordPress的时候是否有对于代码基础优化,当然如果深度优化是需要一些技能的,但是基础的优化我们大家都会做。
在这篇文章中,我们老部落整理几个常用的入门可以提高网站速度和代码优化效率的模块。如果我们是新手WordPress可以添加到网站中然后使得网站效率。
第一、网站目录反斜杠
//~ 页面链接后添加反斜杠
function itbulu_nice_trailingslashit($string, $type_of_url) {
if ($type_of_url != 'single')
$string = trailingslashit($string);
return $string;
}
add_filter('user_trailingslashit', 'itbulu_nice_trailingslashit', 10, 2);
添加这个脚本可以使得网站目录后缀URL加上反斜杠,默认是没有的。比如:https://www.webmuban.com/tutorials/solution/。
第二、禁止emojis
//禁止emojis
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();
}
}
一般网站用不上emojis,我们可以禁止。
第三、去掉CSS/JS后缀版本号
//去除加载的css和js后面的版本号
function sb_remove_script_version( $src ){
$parts = explode( '?', $src );
return $parts[0];
}
add_filter( 'script_loader_src', 'sb_remove_script_version', 15, 1 );
add_filter( 'style_loader_src', 'sb_remove_script_version', 15, 1 );
第四、禁止自PING和版本保存
function no_self_ping( &$links ) {
$home = get_option( 'home' );
foreach ( $links as $l => $link )
if ( 0 === strpos( $link, $home ) )
unset($links[$l]);
}
add_action( 'pre_ping', 'no_self_ping' );
remove_action('pre_post_update', 'wp_save_post_revision');
add_action('wp_print_scripts', 'disable_autosave');
function disable_autosave() {
wp_deregister_script('autosave');
}
第五、部分头部禁止模块
remove_action( 'wp_head', 'wp_generator' );
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', 'feed_links_extra', 3 );
remove_action( 'wp_head', 'feed_links', 2 );
remove_action( 'wp_head', 'rsd_link' );
remove_action( 'wp_head', 'wlwmanifest_link' );
remove_action( 'wp_head', 'index_rel_link' );
remove_action( 'wp_head', 'wp_shortlink_wp_head', 10, 0 );
第六、将JQ文件底部加载
//强制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' );
底部加载可以提高网站打开速度。
第七、关闭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');
第九、移除头部JSON
//移除头部 wp-json 标签和 HTTP header 中的 link Edit By laobuluo.com
remove_action('wp_head', 'rest_output_link_wp_head', 10 );
remove_action('template_redirect', 'rest_output_link_header', 11 );
第十、禁止Gutenberg编辑器
//禁止Gutenberg编辑器
add_filter('use_block_editor_for_post', '__return_false');
remove_action( 'wp_enqueue_scripts', 'wp_common_block_scripts_and_styles' );
总结,我们可以根据上面的功能将代码加入到当前WP主题的Functions.php文件中。
暂无评论...
热评文章
热门文章
暂无文章
热门标签
资源(3) 益智(3) 设计(2) UI(2) 云服务(1) 解密(1) 社交(1) 创意(1) 服务器(1) 美术(1) 颜色(1) 休闲(1) 软件(1) 织梦V6(1) 实用素材(0) 后端(0) 福利(0) unity(0) 美术设计(0) ps(0) 唯美写真(0) ae(0) Photoshop(0) 教程(0) Illustrator(0) wordpress(0) 黑科技(0) ai(0) 前端(0) 编程(0) 一为忆(0) 力学(0) 牛顿(0) 黑洞(0) 望远镜(0) 生命起源(0) 健康(0) 核泄漏(0) 宇宙(0) sad(0) 地方(0) 自行车(0) vbv(0) 摄入(0) 故事感(0) 撒打发(0) 一为(0) 引力波(0)