WordPress-detailed interpretation and principle of commonly used function wp_head()

First, let's take a look at the introduction of wp_heade() and refer to the official documentation for translation.

Usage: wp_head()
Parameter: does not accept any parameters
Return value: NONE
Example: put it in the header.php file, and put it before the </head> tag. Note that it is a PHP function, so use <?php ?>;

OK, knowing the above, let's take a look at the prototype of this function (source file: wp-includes/general-template.php)

<?php
function wp_head() {
	/**
	 * Prints scripts or data in the head tag on the front end.
	 *
	 * @since 1.5.0 //See? This function has been around since version 1.5
	 */
	do_action( 'wp_head' ); //This sentence can be ignored if it is a novice, the scientific name is hook (the concept of hook)
}
?>

 Do you feel a little disappointed when you see this function prototype? Just a word? Yes. But its role in Wordpress is really quite important.

So let's take a look at what this function does.

1. If it is not added, it will cause many plug-ins to fail or even run, because many plug-ins use this function to find the location of <head></head>.

2. If this function is added, a lot of code will be automatically generated in the tag. The most common one is the subscription function. Of course, some functions are not needed by us. Considering optimization, we need to disable it (in detail later)

3. If the plugin or functions function adds css files and js files to the head tag of the theme, they are all output by the wp_head() function.

4. In short, in the theme development or Wordpress second opening, try to add it as much as possible.

5. In fact, while Wordpress is constantly updated, many new functions are completed by relying on this function, such as automatically generating the title of the page (this function is really practical, test version 4.8)

Diverging thinking, can we think of the two functions of wp_footer() and get_sidebar()?

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326243339&siteId=291194637