编写PHP脚本清除WordPress头部冗余代码的方法讲解
《PHP实战:编写PHP脚本清除WordPress头部冗余代码的方法讲解》要点: PHP学习wordpress头部的代码非常多,包括WordPress版本,前后文、第一篇文章、主页meta信息等各种冗余代码,这些对博主来说是没有意义的,也对网站的平安有一定的影响,也一度不知道这些代码是有什么作用、怎么来的和怎么删除. wordpress头部清理代码如下 PHP进修
<?php
//remove_action( 'wp_head','wp_enqueue_scripts',1 );
remove_action( 'wp_head','feed_links',2 );
remove_action( 'wp_head','feed_links_extra',3 );
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','adjacent_posts_rel_link_wp_head',0 );
//remove_action( 'wp_head','locale_stylesheet' );
remove_action( 'publish_future_post','check_and_publish_future_post',1 );
//remove_action( 'wp_head','noindex','wp_print_styles',8 );
//remove_action( 'wp_head','wp_print_head_scripts',9 );
remove_action( 'wp_head','wp_generator' );
//remove_action( 'wp_head','rel_canonical' );
remove_action( 'wp_footer','wp_print_footer_scripts' );
remove_action( 'wp_head','wp_shortlink_wp_head',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'));
}
?>
PHP进修各函数解释: PHP进修wp_head函数 wp_head() 是wordpress的一个非常重要的函数,基本上所有的主题在header.php这个文件里都会使用到这个函数,而且很多插件为了在header上加 点东西也会用到wp_head(),好比SEO的相关插件.但是,在wp_head()出现的这个位置,会增加很多并不常用的代码.可以通过 remove_action移除这些代码. PHP进修remove_action函数 PHP进修函数原型: remove_action( $tag,$function_to_add,$priority,$accepted_args ); 该函数移除一个附属于指定动作hook的函数.该办法可用来移除附属于特定动作hook的默认函数,并可能用其它函数取而代之.参见remove_filter(),add_action() and add_filter().
返回值
移除WordPress版本 PHP进修在head区域,可以看到如下代码: <meta name="generator" content="WordPress 3.1.2" /> 这是隐性显示的WordPress版本信息,默认添加.可以被黑客利用,攻击特定版本的WordPress漏洞.清除代码:
remove_action( 'wp_head','wp_generator' ); 移除离线编纂器开放接口 WordPress自动添加两行离线编纂器的开放接口 <link rel="EditURI" type="application/rsd+xml" title="RSD" href="http://aspzz.cn/xmlrpc.php?rsd" /> <link rel="wlwmanifest" type="application/wlwmanifest+xml" href="http://aspzz.cn/wp-includes/wlwmanifest.xml" /> ? PHP进修
remove_action( 'wp_head','wlwmanifest_link' );
PHP进修移除前后文、第一篇文章、主页meta信息 WordPress把前后文、第一篇文章和主页链接全放在meta中.我认为于SEO赞助不大,反使得头部信息巨大.移除代码: remove_action( 'wp_head','index_rel_link' ); // Removes the index link remove_action( 'wp_head',0 ); // Removes the prev link remove_action( 'wp_head',0 ); // Removes the start link remove_action( 'wp_head',0 ); // Removes the relational links for the posts adjacent to the current post. 移除Canonical标志 09年2月份,Google,Yahoo及Microsoft三大搜索引擎联合推出了一个旨在减少重复内容困扰的办法,这对于广大站长来说不啻是个好事情,不用再担心因为网站上有重复的内容而影响到网站页面的权重了.
即A页面声明B为权威链接,B声明C为权威网页,那么C便是A和B共同的首选权威版本 如果你的WP版本在2.9之前,必要通过插件(上面已经提到)或者手工 Hack 主题的 header.php 文件来使得博客支持. <link rel="canonical" href="<?php get_permalink()?>" /> 在 WordPress 2.9 发布之后,WordPress 已经默认支持这一标签了,我们无需做任何动作,主题就支持这一标签.这对于文章固定链接的更改很有赞助,可以增加对搜索引擎的友好度.但是如果你觉得这个标签对你无用,也可以移除之: PHP进修
remove_action( 'wp_head','rel_canonical' );
PHP进修移除feed HTML 中经由过程 PHP进修
<link rel="alternate" type="application/rss+xml" title="feed名" href="http://aspzz.cn/feed/" />
欢迎参与《PHP实战:编写PHP脚本清除WordPress头部冗余代码的方法讲解》讨论,分享您的想法,编程之家 52php.cn为您提供专业教程。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |