在“讨厌!WordPress头部代码无用标签太多 必须删~”这篇文章中,你可以清理一些对自己“无用”的wordpress头部代码。有的时候,发现这些还不够,最近碰上一个有“代码洁癖”的客户,非要华哥把它网站中的header部分代码清除干净不可。
首要清除的,就是wordpress头部加载的js代码(jquery框架)。那么下面这个实用的代码会移除js代码,并且移除css的链接版本号。
/**
* 移除WordPress加载的JS和CSS链接中的版本号
* http://www.511yj.com/wordpress-js-cssver.html
*/
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 );
哦,差点忘了,这个代码是加在你正在使用的那个wordpress主题文件的functions.php中。
最后清除其他的,或者在if条件下有选择性地进行清除。
参看华哥做清道夫时的所用的functions.php。
// WordPress Emoji Delete
//remove_action( 'wp_head', 'wp_enqueue_scripts', 1 );
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_filter( 'the_content_feed' , 'wp_staticize_emoji');
remove_filter( 'comment_text_rss' , 'wp_staticize_emoji');
remove_filter( 'wp_mail' , 'wp_staticize_emoji_for_email');
remove_action( 'wp_head', 'rsd_link' );
remove_action( 'wp_head', 'feed_links', 2 ); //移除feed
remove_action( 'wp_head', 'feed_links_extra', 3 ); //移除feed
remove_action('wp_head','wlwmanifest_link');//移除head中的rel="wlwmanifest"
remove_action( 'wp_head', 'rest_output_link_wp_head', 10 );
remove_action( 'wp_head', 'wp_oembed_add_discovery_links', 10 );
remove_action( 'wp_head', 'wp_generator' ); //移除WordPress版本
remove_action( 'wp_head', 'rel_canonical' );
remove_action( 'wp_head', 'wp_shortlink_wp_head', 10, 0 );
remove_action( 'wp_head', 'locale_stylesheet' );
add_filter( 'emoji_svg_url', create_function( '', 'return false;' ) );//禁用emoji预解析
//禁止加载WP自带的jquery.js
if ( !is_admin() ) { // 后台不禁止
function my_init_method() {
wp_deregister_script( 'jquery' ); // 取消原有的 jquery 定义
}
add_action('init', 'my_init_method');
}
最后查看网站首页源代码,除了加载所用主题目录下的style.css之外,不会再加载wordpress自动生成的其他头部代码。
真是:白茫茫一片大地真干净!






