Home Backend Development PHP Tutorial 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

May 16, 2016 pm 07:57 PM
php wordpress

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

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

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

移除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地址。

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

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)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

WordPress site file access is restricted: Why is my .txt file not accessible through domain name? WordPress site file access is restricted: Why is my .txt file not accessible through domain name? Apr 01, 2025 pm 03:00 PM

Wordpress site file access is restricted: troubleshooting the reason why .txt file cannot be accessed recently. Some users encountered a problem when configuring the mini program business domain name: �...

See all articles