wordpress头部的代码非常多,包括WordPress版本,前后文、第一篇文章、主页meta信息等各种冗余代码,这些对博主来说是没有意义的,也对网站的安全有一定的影响,也一度不知道这些代码是有什么作用、怎么来的和怎么删除。
wordpress头部清理代码如下
将以下代码插入到你functions.php的文件头部,除WordPress头部大量冗余信息
<?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', 10, 0 ); remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0 ); //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' ); //remove_action( 'wp_head', 'rel_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')); } ?>
各函数解释:
wp_head函数
wp_head() 是wordpress的一个非常重要的函数,基本上所有的主题在header.php这个文件里都会使用到这个函数,而且很多插件为了在header上加 点东西也会用到wp_head(),比如SEO的相关插件。但是,在wp_head()出现的这个位置,会增加很多并不常用的代码。可以通过 remove_action移除这些代码。
remove_action函数
函数原型:
remove_action( $tag, $function_to_add, $priority, $accepted_args );
该函数移除一个附属于指定动作hook的函数。该方法可用来移除附属于特定动作hook的默认函数,并可能用其它函数取而代之。参见remove_filter(), add_action() and add_filter()。
重要:添加hook时的$function_to_remove 和$priority参数要能够相匹配,这样才可以移除hook。该原则也适用于过滤器和动作。移除失败时不进行警告提示。
参数
新闻热点
疑难解答