wp_head()函数

在wp-includes\general-template.php

function wp_head() {
    /**
     * Print scripts or data in the head tag on the front end.
     *
     * @since 1.5.0
     */
    do_action( 'wp_head' );
}

我们经常会在wordpress主题的头部看到wp_head()函数,wp_head()函数是wordpress非常重要的函数,基本上所有的主题在主题头部文件header.php里都会使用到这个函数,同时,许多插件为了在header上加点东西也会用到wp_head()函数,比如网站SEO的相关插件。

但是,在使用wp_head()后,会在网页的头部相应的位置增加很多并不常用的代码,如下面这些代码:

<link rel="alternate" type="application/rss+xml" title="<?php wp_head(); ?>在哪修改?; Feed" href="http://127.0.0.1/feed/" />
<link rel="alternate" type="application/rss+xml" title="<?php wp_head(); ?>在哪修改?; 评论 Feed" href="http://127.0.0.1/comments/feed/" />
<link rel="alternate" type="application/rss+xml" title="<?php wp_head(); ?>在哪修改? 评论 Feed" href="http://127.0.0.1/106.html/feed/" />
<link rel='stylesheet' id='admin-bar-css' href='http://127.0.0.1/wp-includes/css/admin-bar.css?ver=3.4' type='text/css' media='all' />
<link rel="EditURI" type="application/rsd+xml" title="RSD" href="http://127.0.0.1/xmlrpc.php?rsd" />
<link rel="wlwmanifest" type="application/wlwmanifest+xml" href="http://127.0.0.1/wp-includes/wlwmanifest.xml" />
<link rel='prev' title='<?php wp_head(); ?>在哪修改?' href='http://127.0.0.1/105.html' />
<link rel='next' title='<?php wp_head(); ?>在哪修改?' href='http://127.0.0.1/107.html' />
<meta name="generator" content="WordPress 3.4" />
<link rel='canonical' href='http://127.0.0.1/106.html' />
<link rel='shortlink' href='http://127.0.0.1/?p=106' />
<style type="text/css" media="print">#wpadminbar { display:none; }</style>
<style type="text/css" media="screen"> html { margin-top: 28px !important; }
* html body { margin-top: 28px !important; }</style>

这些代码中,可能在我们使用的主题当中用不到,就会形成冗余代码。怎么办呢?这时,我们可以通过 remove_action移除这些代码。方法就是将下面的代码加入主题的functions.php文件中。

//去头部信息remove_action( 'wp_head', 'wp_enqueue_scripts', 1 );
remove_action( 'wp_head', 'feed_links', 2 );//文章和评论feed
remove_action( 'wp_head', 'feed_links_extra', 3 );// 额外的feed,例如category, tag页
remove_action( 'wp_head', 'rsd_link' );//ML-RPC
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 );//rel=pre
remove_action( 'wp_head', 'locale_stylesheet' );
remove_action( 'publish_future_post', 'check_and_publish_future_post', 10, 1 );
remove_action( 'wp_head', 'noindex', 1 );
remove_action( 'wp_head', 'wp_print_styles', 8 );
remove_action( 'wp_head', 'wp_print_head_scripts', 9 );
remove_action( 'wp_head', 'wp_generator' );//移除WordPress版本
//remove_action( 'wp_head', 'rel_canonical' );//移除Canonical标记
remove_action( 'wp_footer', 'wp_print_footer_scripts' );
remove_action( 'wp_head', 'wp_shortlink_wp_head', 10, 0 );
remove_action( 'template_redirect', 'wp_shortlink_header', 11, 0 );
add_action('widgets_init', 'my_remove_recent_comments_style');
function my_remove_recent_comments_style() {
global $wp_widget_factory;
remove_action('wp_head', array($wp_widget_factory->widgets['WP_Widget_Recent_Comments'], 'recent_comments_style'));
}

当然,上面这段代码中有的我们可以需要保留,这时,我们可以在这行代码前加上双斜杠“//”,注释掉这句代码,以免删除掉了我们需要的功能。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值