Home  >  Article  >  Backend Development  >  Explanation on how to write PHP scripts to clear redundant code in WordPress headers_php tips

Explanation on how to write PHP scripts to clear redundant code in WordPress headers_php tips

WBOY
WBOYOriginal
2016-05-16 19:57:451496browse

There are a lot of codes in the WordPress header, including WordPress version, context, first article, homepage meta information and other redundant codes. These are meaningless to bloggers and are detrimental to the security of the website. To a certain extent, I also didn’t know what these codes did, where they came from, and how to delete them.

Wordpress header cleaning code is as follows
Insert the following code into the header of your functions.php file, except for the large amount of redundant information in the WordPress header

<&#63;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')); 
} 
&#63;>

Explanation of each function:

wp_head function

wp_head() is a very important function in WordPress. Basically all themes will use this function in the header.php file, and many plug-ins will also use wp_head() to add something to the header. For example, SEO related plug-ins. However, where wp_head() appears, a lot of code that is not commonly used will be added. These codes can be removed via remove_action.

remove_action function

Function prototype:

remove_action( $tag, $function_to_add, $priority, $accepted_args );

This function removes a function attached to the specified action hook. This method can be used to remove the default function attached to a specific action hook, and possibly replace it with another function. See remove_filter(), add_action() and add_filter().
Important: The $function_to_remove and $priority parameters when adding a hook must match so that the hook can be removed. This principle also applies to filters and actions. No warning will be given when removal fails.
Parameters

  • $tag (string) (required) The action hook to which the function to be deleted is connected. Default value: None
  • $function_to_remove (callback) (required) The name of the function to be removed Default value: None
  • $priority (integer) (optional) Function priority (defined when the function is initially connected) Default: 10
  • $accepted_args (integer) (required) The number of arguments accepted by the function. Default value: 1

Return value

  • (boolean) Whether the function is removed.
  • Ttue function was successfully removed
  • False function has not been removed

Remove WordPress version

In the head area, you can see the following code:

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

This is the WordPress version information displayed implicitly and added by default. Vulnerabilities that can be exploited by hackers to attack specific versions of WordPress. Clear code:
remove_action( 'wp_head', 'wp_generator' );

Remove the offline editor open interface

WordPress automatically adds an open interface for a two-line offline editor

<link rel="EditURI" type="application/rsd+xml" title="RSD" href="http://jb51.net/xmlrpc.php&#63;rsd" /> 
<link rel="wlwmanifest" type="application/wlwmanifest+xml" href="http://jb51.net/wp-includes/wlwmanifest.xml" />


Among them, RSD is a generalized interface, and wlwmanifest is for Microsoft Live Writer editor. If you don't need offline editing, you can remove it. Even if you need to use an offline editor, most of the time you don't need these two lines of code. Live Writer knows them himself. Keeping these two lines of code may leave security risks. Clear code:

remove_action( 'wp_head', 'rsd_link' ); 
remove_action( 'wp_head', 'wlwmanifest_link' );

Remove the context, first article, and homepage meta information

WordPress puts all the context, first article and homepage link in meta. I don’t think it helps much with SEO, but it makes the header information huge. Remove code:

remove_action( 'wp_head', 'index_rel_link' ); // Removes the index link 
remove_action( 'wp_head', 'parent_post_rel_link', 10, 0 ); // Removes the prev link 
remove_action( 'wp_head', 'start_post_rel_link', 10, 0 ); // Removes the start link 
remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0 ); // Removes the relational links for the posts adjacent to the current post.

Remove Canonical Mark

In February 2009, the three major search engines Google, Yahoo and Microsoft jointly launched a method aimed at reducing the problem of duplicate content. This is a good thing for the majority of webmasters. They no longer have to worry about duplication on the website. The content affects the weight of the website page.
There are many reasons for duplicate content. The most common one is that multiple URL addresses point to the same page. For example, a blog page under the WordPress platform includes articles and comments. Each comment can have a fixed link address. If there are multiple comments, the link of each comment will be similar to the above format, except that the commentID number is different. These links actually point to the same article. When a spider comes to crawl, it will crawl again in sequence. If there are 10 comments under this article, the same page article will be crawled 10 times, which is equivalent to doing multiple repetitive tasks, seriously affecting the crawling efficiency, and Bandwidth is consumed.
The result of duplicate content is that spiders are unwilling to crawl. Different URLs pointing to the same page will also affect the weight of the page. Such problems can be effectively avoided through canonical tags.
Two points need to be noted:

  • It is allowed to point to different subdomains, but not to other domain names
  • canonical attributes can be passed

That is, page A declares B as an authoritative link, and B declares C as an authoritative web page, then C is the preferred authoritative version of both A and B

If your WP version is before 2.9, you need to enable blog support through a plug-in (already mentioned above) or manually Hack the header.php file of the theme.

<link rel="canonical" href="<&#63;php get_permalink()&#63;>" />

在 WordPress 2.9 发布之后,WordPress 已经默认支持这一标签了,我们无需做任何动作,主题就支持这一标签。这对于文章固定链接的更改很有帮助,可以增加对搜索引擎的友好度。但是如果你觉得这个标签对你无用,也可以移除之:

remove_action( 'wp_head', 'rel_canonical' );

移除feed

HTML 中通过

<link rel="alternate" type="application/rss+xml" title="feed名" href="http://jb51.net/feed/" />

来指定博客feed。可以被浏览器检测到,然后被读者订阅。
如果你不想添加feed,或者想使用烧制的feed(如FeedSky或者Feedburner烧制的feed),可以移除之。

remove_action( 'wp_head', 'feed_links', 2 );//文章和评论feed 
remove_action( 'wp_head', 'feed_links_extra', 3 ); //分类等feed

如果用的烧制的feed,然后还可以再手动添加feed地址。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn