6个优化WordPress程序精简代码提高打开速度

WordPress程序用来建站确实是不错的选择,但是他们的缺点就是需要后期不断的运维优化。但是对于很多新手用户来说,你让他们优化程序也是不现实的。这里我们可以采用前人优化经验和易操作的教程来解决问题,比如前期我们只要简单的安装一个缓存插件,以及优化程序代码就可以。

下面提供的6个常用优化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’s

//禁用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 中,优化精简代码,提高Wordpress速度。

投票 post
© 版权声明
THE END
喜欢就支持一下吧
点赞15 分享