1. 首页
  2. 技术知识

【WordPress相关】WordPress 技巧:删除 wp_head 中无关紧要的代码

作品分类:Wordpress相关  移除  代码  日志  无关紧要  删除  代码  技巧  WordPress

WordPress 技巧:删除 wp_head 中无关紧要的代码,

WordPress 通过 wp_head() 在页面的头部输出了很多东西,但是这些标签很多是没用的,虽然这些代码也没有什么副作用,但是个人更喜欢保持一个简洁的 header。

所以这篇日志将教你如何移除 header 中的下面几个标签,你可以根据自己的需求选择移除:

<script async src=”//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js”></script> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script>

  • Really Simple Discovery (RSD) link
  • Windows Live Writer link
  • WordPress generator 信息
  • 和日志相关的 Link

Really Simple Discovery

输出代码如下:

<link rel="EditURI" type="application/rsd+xml" title="RSD" href="http://example.com/xmlrpc.php?rsd" />

这是 XML-RPC 客户端发现机制需要用到的,如果你不知道这个是什么意思,或者没有集成类似 Flickr 这类服务到你的站点,那么你可以安全的移除它:

remove_action('wp_head', 'rsd_link');

Windows Live Writer

输出代码如下:

<link rel="wlwmanifest" type="application/wlwmanifest+xml" href="http://example.com/wp-includes/wlwmanifest.xml" />

如果你没有使用 Windows Live Writer 来写日志,那就移除它吧:

remove_action('wp_head', 'wlwmanifest_link');

WordPress Generator

它是用来在 header 显示你的 WordPress 版本号:

<meta name="generator" content="WordPress 3.5.1" />

你自己当然知道你所使用的 WordPress 版本了,并且给坏人知道,还能造成安全漏洞:

remove_action('wp_head', 'wp_generator');

和日志相关的 Link

Post relational links(和日志相关的 Link)即使下面这一堆:

<link rel='index' title='Main Page' href='http://blog.wpjam.com' />
<link rel='start' title='Article in the distant past' href='http://blog.wpjam.com/hello-world/' />
<link rel='prev' title='The Post Before This One' href='http://blog.wpjam.com/post-before/' />
<link rel='next' title='The Post After This One' href='http://blog.wpjam.com/post-after/' />

一些浏览器可以通过这些代码进行导航,但是一个设计优秀的主题同样可以做到,所以移除它们:

remove_action('wp_head', 'start_post_rel_link');
remove_action('wp_head', 'index_rel_link');
remove_action('wp_head', 'adjacent_posts_rel_link');

汇总

汇总一次,将上面所有代码都复制到 functiоns.php 就能一次性移除了。

<?php
remove_action('wp_head', 'rsd_link');
remove_action('wp_head', 'wlwmanifest_link');
remove_action('wp_head', 'wp_generator');
remove_action('wp_head', 'start_post_rel_link');
remove_action('wp_head', 'index_rel_link');
remove_action('wp_head', 'adjacent_posts_rel_link');
?>

该功能已经整合到 插件中,你只需要勾选下就可以移除。


本站推荐使用的主机:,国外主机建议使用

原创文章,作者:starterknow,如若转载,请注明出处:https://www.starterknow.com/32800.html

联系我们