Explanation on how to write PHP script to remove redundant code in WordPress header, wordpress redundancy_PHP tutorial

WBOY
Release: 2016-07-12 08:58:10
Original
970 people have browsed it

Explanation on how to write a PHP script to remove redundant code in the WordPress header. WordPress redundancy

There are a lot of codes in the WordPress header, including the WordPress version, the following and the first article Various redundant codes such as articles and homepage meta information are meaningless to bloggers and have a certain impact on the security of the website. At one time, I didn’t know what these codes did, how they came about, 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;>
Copy after login

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 );
Copy after login

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" />

Copy after login

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

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" />
Copy after login


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' );
Copy after login

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.
Copy after login

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;>" />
Copy after login

After the release of WordPress 2.9, WordPress already supports this tag by default. We don’t need to do anything for the theme to support this tag. This is very helpful for changing the permalink of the article and can increase the friendliness of the search engine. But if you feel that this tag is of no use to you, you can also remove it:

remove_action( 'wp_head', 'rel_canonical' );
Copy after login

Remove feed

HTML 中通过

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

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

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

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

您可能感兴趣的文章:

  • 是 WordPress 让 PHP 更流行了 而不是框架
  • WordPress主题制作中自定义头部的相关PHP函数解析
  • WordPress中调试缩略图的相关PHP函数使用解析
  • WordPress开发中用于获取近期文章的PHP函数使用解析
  • WordPress开发中自定义菜单的相关PHP函数使用简介
  • WordPress中用于获取搜索表单的PHP函数使用解析
  • 详解WordPress中创建和添加过滤器的相关PHP函数
  • WordPress中用于创建以及获取侧边栏的PHP函数讲解
  • WordPress中编写自定义存储字段的相关PHP函数解析
  • WordPress中用于更新伪静态规则的PHP代码实例讲解

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1105378.htmlTechArticle编写PHP脚本清除WordPress头部冗余代码的方法讲解,wordpress冗余 wordpress头部的代码非常多,包括WordPress版本,前后文、第一篇文章、主页me...
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!