search
HomeBackend DevelopmentPHP TutorialExplanation on how to write PHP script to remove redundant code in WordPress header, wordpress redundancy_PHP tutorial

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;>

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 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' );

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

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' );

Remove 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地址。

您可能感兴趣的文章:

  • 是 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...
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
php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

php怎么实现几秒后执行一个函数php怎么实现几秒后执行一个函数Apr 24, 2022 pm 01:12 PM

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php字符串有没有下标php字符串有没有下标Apr 24, 2022 am 11:49 AM

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php怎么除以100保留两位小数php怎么除以100保留两位小数Apr 22, 2022 pm 06:23 PM

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

php怎么根据年月日判断是一年的第几天php怎么根据年月日判断是一年的第几天Apr 22, 2022 pm 05:02 PM

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php怎么读取字符串后几个字符php怎么读取字符串后几个字符Apr 22, 2022 pm 08:31 PM

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

php怎么替换nbsp空格符php怎么替换nbsp空格符Apr 24, 2022 pm 02:55 PM

方法:1、用“str_replace("&nbsp;","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\&nbsp\;||\xc2\xa0)/","其他字符",$str)”语句。

php怎么查找字符串是第几位php怎么查找字符串是第几位Apr 22, 2022 pm 06:48 PM

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools