老肖博客
WEB开发技术与AI应用 1014 次浏览 2 分钟阅读

讨厌!WordPress头部代码无用标签太多 必须删~

目录
  • 去除WORDPRESS自带的 Emoji
  • 去除 XMLRPC, WLW, Generator, Feeds 和 ShortLink
  • 去除 Previous 和 Next 文章链接
  • 去除 XFN (XHTML Friends Network) Profile 链接 和 Pingback URL
  • 用WordPress建站,方是方便,但由于WordPress是一个国际化的软件,在网页头部代码里会产生很多符合国际标准的标签(如果你用了国人设计的模板,此现象不易出现),这些标签每个都有自己的意义和用途,但在中国的互联网环境下,则有很多是用不到的。如果在网站页面里加载太多的标签,会影响页面的加载速度,如果确定无用的,手狠一点,该删除且删除。

    那到底哪些头部标签可以删除呢?华哥从自己的几个网站中,找出大部分在用的。

    去除WORDPRESS自带的 Emoji

    // WordPress Emoji Delete
    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');
    add_filter( 'emoji_svg_url', create_function( '', 'return false;' ) );//禁用emoji预解析
    

    去除 XMLRPC, WLW, Generator, Feeds 和 ShortLink

    去掉这些标记,向 function.php(在你所使用的模板主题目录下) 文件里添加下面的代码:

    remove_action('wp_head', 'rsd_link'); //removes EditURI/RSD (Really Simple Discovery) link.
    remove_action('wp_head', 'wlwmanifest_link'); //removes wlwmanifest (Windows Live Writer) link.
    remove_action('wp_head', 'wp_generator'); //removes meta name generator.
    remove_action('wp_head', 'wp_shortlink_wp_head'); //removes shortlink.
    remove_action( 'wp_head', 'feed_links', 2 ); //removes feed links.
    remove_action('wp_head', 'feed_links_extra', 3 );  //removes comments feed.
    

    去除 Previous 和 Next 文章链接

    去掉文章的上一篇和下一篇链接,向 function.php 文件里添加下面的代码:

    /*Removes prev and next links*/
    remove_action('wp_head', 'adjacent_posts_rel_link_wp_head');

    去除 XFN (XHTML Friends Network) Profile 链接 和 Pingback URL

    这个 rel=profile 链接和 rel=Pingback 标记可以从header.php文件里直接删除。做法是,打开Wordperss主题里的header.php文件,删除下面两行:

    <link rel="profile" href="http://gmpg.org/xfn/11"> 
    <link rel="pingback" href="<?php%20bloginfo(%20'pingback_url'%20);%20?>">

    禁用REST API功能

    //禁用REST API功能代码
    add_filter('rest_enabled', '__return_false');
    add_filter('rest_jsonp_enabled', '__return_false');
    

    移除wp-json链接

    //移除wp-json链接的代码
    remove_action( 'wp_head', 'rest_output_link_wp_head', 10 );
    remove_action( 'wp_head', 'wp_oembed_add_discovery_links', 10 );
    

    根据上边的内容,对比检查自己WORDPRESS网站首页源代码里的头部标签(head区),有选择地删除即可。

    更新于

    相关文章