探索建站和运维乐趣

10个WordPress必备提高WordPress访问速度和精简代码

我们很多网友,尤其是很多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.laobuluo.com/tutorials/solution/。

第二、禁止emojis

//禁止emojis Edit By laobuluo.com
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后面的版本号 Edit By laobuluo.com
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库文件底部载入 Edit By laobuluo.com
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 功能  Edit By laobuluo.com
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文件中。

赞(1)
转载保留:老部落 » 10个WordPress必备提高WordPress访问速度和精简代码


关注公众号『老蒋朋友圈』

获取更多建站运营运维新知!
互联网创业、前沿技术......