Write a PHP script to clear the WordPress header redundancy code.
There are a lot of code in the wordpress header, including the WordPress version, the prefix, the first article, the meta information on the homepage, and other redundant code, which is meaningless to the blogger, it also has a certain impact on the security of the website, and once did not know what the code is for, how to come, and how to delete it.
The wordpress header cleanup code is as follows:
Insert the following code into the file header of your functions. php file. In addition to a large amount of redundant information in the WordPress header
<?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')); } ?>
Function Description:
Wp_head Function
Wp_head () is a very important function of wordpress. Basically, all themes are in the header. this function is used in the php file, and many plug-ins also use wp_head () to add something to the header, such as SEO-related plug-ins. However, the position where wp_head () appears will add a lot of less commonly used code. You can use remove_action to remove the code.
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 functions attached to the hook of a specific action, and may be replaced by other functions. See remove_filter (), add_action () and add_filter ().
Important: The $ function_to_remove and $ priority parameters of the hook must match to remove the hook. This principle also applies to filters and actions. No warning is prompted when the 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) Name of the function to be deleted default value: None
- $ Priority (integer) (optional) function priority (defined when the function is initially connected) Default Value: 10
- $ Accepted_args (integer) (required) number of parameters accepted by the function. Default Value: 1
Return Value
- (Boolean) whether the function is removed.
- The Ttue function is successfully removed.
- The False function is not 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 hidden display of WordPress version information, which is added by default. Attackers can exploit this vulnerability to attack a specific version of WordPress. Clear code:
remove_action( 'wp_head', 'wp_generator' );
Remove offline editor APIs
WordPress automatically adds two open interfaces for offline editors
<link rel="EditURI" type="application/rsd+xml" title="RSD" href="http://jb51.net/xmlrpc.php?rsd" /> <link rel="wlwmanifest" type="application/wlwmanifest+xml" href="http://jb51.net/wp-includes/wlwmanifest.xml" />
In this example, RSD is a generalized interface, and wlwmanifest is for the Microsoft Live Writer editor. If you do not need to edit it offline, you can remove it. Even if you need an offline Editor, most of the time you do not need the two lines of code. Live Writer knows them. 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 text, first article, and homepage meta information
WordPress stores all the text, first article, and homepage links in meta. I don't think SEO is helpful, 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 flag
In September February, Google, Yahoo, and Microsoft jointly launched a method designed to reduce repeated content troubles, which is a good thing for webmasters, there is no need to worry that duplicate content on the website will affect the weight of the website page.
There are many reasons for duplicate content. The most common reason is that multiple URLs point to the same page, for example, a log page under the wordpress platform, including the article and comments. Each comment can have a fixed link address. If there are multiple comments, the links of each comment are similar to the above format, but the commentID number is different, these links actually point to the same chapter. When a spider crawls, the spider crawls the pages one by one. If there are 10 comments in this article, the spider crawls the same page article 10 times, which is equivalent to repeating the work multiple times, it seriously affects the capture efficiency and bandwidth consumption.
The result of repeated content is that the SPIDER does not want to crawl. Different URLs direct to the same page, which also affects the weight of the page. Canonical labels can effectively avoid such problems.
Pay attention to the following two points:
- It can direct to different subdomain names, not to other domain names
- Canonical attributes can be passed
That is, if page A declares that page B is an authoritative link and page B declares that Page C is an authoritative webpage, C is the preferred authoritative version for both A and B.
If your WP version is earlier than 2.9, you need to use the plug-in (as mentioned above) or the header. php file of the manual Hack topic to support your blog.
<link rel="canonical" href="<?php get_permalink()?>" />
After WordPress 2.9 is released, WordPress supports this tag by default. You do not need to perform any actions, and the topic supports this tag. This is helpful for changing the fixed link of the article and increases the friendliness of the search engine. But if you think this label is useless to you, you can also remove it:
remove_action( 'wp_head', 'rel_canonical' );
Remove feed
In HTML
<Link rel = "alternate" type = "application/rss + xml" title = "feed name" href = "http://jb51.net/feed/"/>
To specify the blog feed. It can be detected by the browser and subscribed by the reader.
If you do not want to add a feed or want to use a burned feed (such as a feed fired by FeedSky or Feedburner), you can remove it.
Remove_action ('wp _ head', 'feed _ links ', 2); // articles and comments feed remove_action ('wp _ head', 'feed _ links_extra', 3 ); // category and other feeds
If you use a burned feed, you can also manually add the feed address.
Articles you may be interested in:
- WordPress makes PHP more popular than the framework
- Analysis of PHP functions related to custom headers in WordPress topic Creation
- Use of PHP functions for debugging thumbnails in WordPress
- Analysis of PHP functions used for obtaining recent articles in WordPress Development
- Introduction to using custom menu related PHP functions in WordPress Development
- Use of PHP functions used to retrieve search forms in WordPress
- Describes the PHP functions used to create and add filters in WordPress.
- PHP functions used to create and retrieve sidebar in WordPress
- PHP function parsing for writing custom storage fields in WordPress
- PHP code example for updating pseudo-static rules in WordPress