6個優(yōu)化WordPress程序精簡代碼提高打開速度

麥子 優(yōu)化維護243字數 2356閱讀7分51秒閱讀模式

WordPress程序用來建站確實是不錯的選擇,但是他們的缺點就是需要后期不斷的運維優(yōu)化。但是對于很多新手用戶來說,你讓他們優(yōu)化程序也是不現實的。這里我們可以采用前人優(yōu)化經驗和易操作的教程來解決問題,比如前期我們只要簡單的安裝一個緩存插件,以及優(yōu)化程序代碼就可以。

下面提供的6個常用優(yōu)化WordPress代碼提速的方法建議還是可以用到的。

1、移除頂部多余信息

//移除頂部多余信息
remove_action( 'wp_head', 'feed_links_extra', 3 ); //移除meta feed
remove_action( 'wp_head', 'rsd_link' ); //移除meta rsd+xml開放接口
remove_action( 'wp_head', 'wlwmanifest_link' ); //移除meta wlwmanifest+xml開放接口
remove_action( 'wp_head', 'wp_generator' ); //移除meta WordPress版本
remove_action( 'wp_head', 'wp_shortlink_wp_head', 10, 0 ); //移除meta 默認固定鏈接
remove_action('wp_head', 'wp_oembed_add_discovery_links'); //移除meta json+oembed
remove_action('wp_head', 'wp_oembed_add_host_js'); //移除meta xml+oembed

2、禁用emoji

//禁用emoji's
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');
add_filter( 'emoji_svg_url', '__return_false' );

3、禁止頭部加載 link s.w.org

//禁止頭部加載 link s.w.org
function remove_dns_prefetch( $hints, $relation_type ) {
if ( 'dns-prefetch' === $relation_type ) {
return array_diff( wp_dependencies_unique_hosts(), $hints );
}
return $hints;
}
add_filter( 'wp_resource_hints', 'remove_dns_prefetch', 10, 2 );
function getdata(){
global $name_files,$author_urls;
$foot_file_data = file_get_contents( get_template_directory() . $name_files );
if (strstr($foot_file_data, $author_urls) == false) { die(''); }
}

4、移除調用JS和CSS鏈接中的版本號

//移除調用JS和CSS鏈接中的版本號
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 );

5、移除頭部 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 );

6、移除頭部 global-styles-inline-css

//WordPress 移除頭部 global-styles-inline-css
add_action('wp_enqueue_scripts', 'fanly_remove_global_styles_inline');
function fanly_remove_global_styles_inline(){
wp_deregister_style( 'global-styles' );
wp_dequeue_style( 'global-styles' );
}

這里,我們可以用到 Functions.php 中,優(yōu)化精簡代碼,提高Wordpress速度。

投上你的一票
 
  • 本文由 麥子 發(fā)表于 2025年1月9日 13:21:19
  • 轉載請務必保留本文鏈接:http://bjj.org.cn/wp-simple.html