wp头部总是有一大堆不必要的样式和js,都是一些插件的或者系统自带的,看起来很头疼,网站加载起来也很缓慢,是时候把他们全部切除删掉了,优化我们的主题,让网站更好更快更利于收录!
老规矩,添加到functions.php文件里面:
function disable_emojis() {
remove_action( 'wp_head', 'wp_generator' ); //移除WordPress版本
remove_action( 'wp_head', 'rsd_link' ); //移除离线编辑器开放接口
remove_action( 'wp_head', 'wlwmanifest_link' ); //移除离线编辑器开放接口
remove_action( 'wp_head', 'index_rel_link' ); //去除本页唯一链接信息
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', 2 ); //移除feed
remove_action( 'wp_head', 'feed_links_extra', 3 ); //移除feed
remove_action( 'wp_head', 'rest_output_link_wp_head', 10 ); //移除wp-json链
remove_action( 'wp_head', 'print_emoji_detection_script', 7 ); //头部的JS代码
remove_action( 'wp_head', 'rel_canonical' ); //rel=canonical
remove_action( 'wp_head', 'wp_shortlink_wp_head', 10, 0 ); //rel=shortlink
remove_action( 'wp_head', 'wp_oembed_add_discovery_links' );
remove_action('wp_head','_admin_bar_bump_cb');
add_filter( 'wp_resource_hints', 'remove_dns_prefetch', 10, 2 ); //头部加载DNS预获取(dns-prefetch
remove_filter('the_content', 'wptexturize');//禁止代码标点转换
}
add_action( 'init', 'disable_emojis' );
//移除WordPress头部加载DNS预获取(dns-prefetch)
function remove_dns_prefetch( $hints, $relation_type ) {
if ( 'dns-prefetch' === $relation_type ) {
return array_diff( wp_dependencies_unique_hosts(), $hints );
}
return $hints;
}
补充:
禁止正文符号转义
remove_filter('the_content', 'wptexturize');
//取消摘要转义
remove_filter('the_excerpt', 'wptexturize');
//取消评论转义
remove_filter('comment_text', 'wptexturize');
//删除wordpress默认相册样式
add_filter('gallery_style',
create_function('$css', 'return preg_replace("#<style type=\'text/css\'>(.*?)</style>#s", "", $css);')
);
// 去除后台标题 wordpress字样
add_filter('admin_title', function ($admin_title, $title){
return $title.' ‹ '.get_bloginfo('name');
}, 10, 2);