WordPress wp_head () Optimization: Remove unnecessary element tags

Source: Internet
Author: User
Tags vars wordpress version

Recently, the author in the use of WordPress to build a station, found that the page will generate a lot of redundant code, some things we do not use, and there is no big role, the code I find a half-day also did not find the source code how to delete, the final discovery is wp_head () This method outputs the code, then how to delete these unnecessary header information. article from http://www.life134.com

Remove unnecessary element tags from WordPress head article from http://www.life134.com

Complete WordPress Header Cleanup code

  1. <?php
  2. Remove_action (' Wp_head ', ' wp_enqueue_scripts ', 1);   
  3. Remove_action (' Wp_head ', ' feed_links ', 2);
  4. Remove_action (' Wp_head ', ' Feed_links_extra ', 3);
  5. Remove_action (' Wp_head ', ' rsd_link ');
  6. Remove_action (' Wp_head ', ' wlwmanifest_link ');
  7. Remove_action (' Wp_head ', ' index_rel_link ');
  8. Remove_action (' Wp_head ', ' Parent_post_rel_link ', 10, 0);
  9. Remove_action (' Wp_head ', ' Start_post_rel_link ', 10, 0);
  10. Remove_action (' Wp_head ', ' adjacent_posts_rel_link_wp_head ', 10, 0);
  11. Remove_action (' Wp_head ', ' locale_stylesheet ');   
  12. Remove_action (' Publish_future_post ', ' check_and_publish_future_post ', 10, 1);
  13. Remove_action (' Wp_head ', ' noindex ', 1);   
  14. Remove_action (' Wp_head ', ' wp_print_styles ', 8);   
  15. Remove_action (' Wp_head ', ' wp_print_head_scripts ', 9);   
  16. Remove_action (' Wp_head ', ' wp_generator ');
  17. Remove_action (' Wp_head ', ' rel_canonical ');   
  18. Remove_action (' Wp_footer ', ' wp_print_footer_scripts ');
  19. Remove_action (' Wp_head ', ' wp_shortlink_wp_head ', 10, 0);
  20. Remove_action (' Template_redirect ', ' Wp_shortlink_header ', 11, 0);
  21. Add_action (' Widgets_init ', ' My_remove_recent_comments_style ');
  22. function My_remove_recent_comments_style () {
  23. Global $WP _widget_factory;
  24. Remove_action (' Wp_head ', array ($wp _widget_factory->widgets[' wp_widget_recent_comments '), ' Recent_   Comments_style '));
  25. }
  26. ?>

Insert this code into the theme of the functions.php file, you can clear the WordPress head a lot of redundant information. Let's talk about what this code means to avoid removing some of the features you want to keep.

Wp_head () function

Wp_head () is a very important function of WordPress, basically all the theme in header.php this file will use this function, and many plugins in order to add something in the header will also use the Wp_head (), such as the relevant plug-ins SEO. However, in the Wp_head (), this position will add a lot of infrequently used code, how to delete it? You can remove the code through remove_action.

remove_action function

Function prototypes: Remove_action ($tag, $function _to_add, $priority, $accepted _args);

The function removes a function attached to the specified action hook. This method can be used to remove the default function attached to a particular action hook and possibly replace it with another function.

Important: When adding hooks, the $function_to_remove and $priority parameters should be matched so that hooks can be removed. This principle also applies to filters and actions. No warning prompts when removing a failure. article from http://www.life134.com

Parameters article from http://www.life134.com

  • 1. $tag (string) (required) The action hook to which the function being removed will be connected. Default value: None
  • 2. $function _to_remove (callback) (required) The name of the function to be deleted default value: None
  • 3. $priority (integer) (optional) function precedence (defined when function is initially connected) default value: 10
  • 4. $accepted the number of parameters accepted by the _args (integer) (required) function. Default value: 1

return value

Whether or not the (Boolean) function has been removed. article from http://www.life134.com

  • 1.Ttue function was successfully removed
  • 2.False function Not removed

Remove WordPress Version Information

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

    1. <Meta name="generator" content="WordPress 3.1.3" />

This is the hidden display of WordPress version information, added by default. Can be exploited by hackers to attack a specific version of the WordPress vulnerability. Clear code:

    1. Remove_action (' Wp_head ', ' wp_generator ');

Remove Offline Editor Open Interface

WordPress automatically add two lines of Offline editor open Interface

    1. <link  rel= "Edituri"  type= "application/rsd+xml"  title= "RSD"  href= "/http" EXAMPLE.COM/XMLRPC.PHP?RSD " />   
    2. <link rel= "wlwmanifest"  type=" application/wlwmanifest+xml " href= "http://example.com/wp-includes/wlwmanifest.xml"  />  

Where RSD is a generalized interface, Wlwmanifest is for the Microsoft Live Writer editor. If you don't need to edit offline, you can remove it. Even if you need to use an offline editor, these two lines of code are not required most of the time. Live writer knows them by himself. Keeping these two lines of code may leave a security risk. Clear code:

    1. Remove_action (' Wp_head ', ' rsd_link ');
    2. Remove_action (' Wp_head ', ' wlwmanifest_link ');

Remove contextual, first article, home page meta information

WordPress puts contextual, the first article and the homepage link in Meta. I think the SEO help is not big, reverse makes the head information huge. Remove code: article from http://www.life134.com

  1. Remove_action (' Wp_head ', ' index_rel_link '); //Removes the index link
  2. Remove_action (' Wp_head ', ' Parent_post_rel_link ', 10, 0); //Removes the prev link
  3. Remove_action (' Wp_head ', ' Start_post_rel_link ', 10, 0); //Removes the start link
  4. 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 tags

In February 09, Google,yahoo and Microsoft launched a joint effort to reduce the number of repetitive content, which is a good thing for the webmaster, no longer worry because the site has duplicate content and affect the weight of the site page.

There are many reasons for duplicate content, the most common is that multiple URL addresses to the same page, such as: WordPress platform under a log page, including articles and comments content. Each comment can have a fixed link address, if there are multiple comments, then each comment link is similar to the above format, but the Commentid number is different, these links are actually pointing to the same article. When spiders come crawling, they will crawl again, this article if there are 10 comments, then climbed 10 times the same page article, equivalent to do a number of repeated work, seriously affected the efficiency of the crawl, and the cost of bandwidth.

The result of repeating content is necessarily that spiders do not want to crawl, different URLs point to the same page, it will affect the weight of the page. Through the canonical tag, this kind of problem can be effectively avoided. article from http://www.life134.com

There are two points to note:

    • 1. Allow pointing to different subdomains, not to other domain names
    • 2.canonical properties can be passed
      That is, a page declaration b is authoritative link, B declaration c is authoritative web page, then C is a and B common preferred authoritative version

If your WP version before 2.9, you need to use the plugin (mentioned above) or manually Hack the theme of the header.php file to make the blog support.

    1. <link rel="canonical" Href="<?php get_permalink ()?>"/>

After WordPress 2.9 was released, WordPress has already supported this tag by default, and we do not need to do any action, the theme supports this tag. This is useful for changing the article permalink, which can increase the friendliness of the search engine. But if you think this tag is useless to you, you can remove it:

    1. Remove_action (' Wp_head ', ' rel_canonical ');

Remove Feed

HTML via <link rel= "alternate" type= "Application/rss+xml" title= "Feed name" href= "http://example.com/feed/"/> To specify a blog feed. Can be detected by the browser and then subscribed by the reader.

If you don't want to add a feed, or want to use a fired feed (such as a Feedsky or FeedBurner-fired feed), you can remove it.

    1. Remove_action (' Wp_head ', ' feed_links ', 2); //articles and comments feed
    2. Remove_action (' Wp_head ', ' Feed_links_extra ', 3); //categories and other feeds

If you use a fired feed, you can then add the feed address manually.

WordPress wp_head () Optimization: Remove unnecessary element tags (go)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.