2 hours ago
background
As an excellent open source project, wordpress is widely used in the world. However, in the wordpress framework, global is often used for assignment, so programmers who use it for the first time cannot quickly have an intuitive understanding of its overall architecture. After a special friend entered a company that uses WordPress, he was slow to get familiar with WordPress and helped to sort it out; Here, I will record the process of sorting.
Three stages
First, let's take a general look at the three main processes of wordpress: initialization (resource loading), data preparation, and rendering.
As we all know, the main entry file of wordpress is index.php. The file wp-blog-header.php is introduced into index.php.
// wp-blog-header.php if ( ! isset( $wp_did_header ) ) {
$wp_did_header = true;
// Load the WordPress library.
require_once __DIR__ . / wp-load.php;
// Set up the WordPress query.
wp();
// Load the theme template.
require_once ABSPATH . WPINC . / template-loader.php;
}
This file contains three stages of wordpress loading:
Resource loading phase:
require_once __DIR__ . / wp-load.php;
Data preparation stage:
// Set up the WordPress query.
wp();
Rendering phase:
// Load the theme template.
require_once ABSPATH . WPINC . / template-loader.php;
Next, let's look at what is done at each stage.
Resource loading stage
Wp-load.php introduces wp-config.php, wp-config.php introduces wp-settings.php, and the core load logic is in wp-settings.php.
In the resource loading phase, it mainly loads all dependent PHP libraries and files, including various core classes, functions, plug-ins, etc.
It should be noted that there is no wp-config.php file when WordPress is not successfully installed.
Data preparation stage
The entry to the data preparation phase is the wp() function in wp blog header. php; The wp function is defined in functions.php; Finally, the main () method in class-wp.php will be called.
The main() method mainly does three things:
Parse_request(): parse the requested parameterquery_posts(): query the article register_globals() through the requested parameters: register the global data so that the data can be obtained in the rendering phase
We focus on the second and third issues.
Query article
Query_posts() will call the query() method in class-wp-query.php. The query() method does two things:
Init(): reset all data get_posts(): query articles
The get_posts() method will call the parse_query() method of the current class. This method is close to 1000 lines and is the core logic in the whole wordpress.
The parse_query() logic is complex. We only focus on two things:
Parse the requested data (get/post), construct the read/write data and conditions, including date data article title attachments.... Other conditions determine which page the current request is based on the url request parameters. For example, setting is_home=true indicates the home page, and is_page=true indicates the article page; Set the current page, which will be used in the data rendering phase.
The articles queried by the parse_query() method will be placed in the posts attribute and post attribute of the current class. If we need to debug, we can dump the sql queried at the end of this method
var_dump($where . $groupby . $orderby . $limits . $join);
After the get_posts() execution is completed, we also get the desired article (queried according to the url parameter), and then enter the phase of registering global data, namely register_globals();
The code of register_globals() method is relatively simple, as follows:
public function register_globals() { global $wp_query;
// Extract updated query vars back into global namespace.
foreach ( (array) $wp_query->query_vars as $key => $value ) {
$GLOBALS[ $key ] = $value;
}
$GLOBALS[query_string] = $this->query_string;
$GLOBALS[posts] = & $wp_query->posts;
$GLOBALS[post] = isset( $wp_query->post ) ? $ wp_query->post : null;
$GLOBALS[request] = $wp_query->request;
if ( $wp_query->is_single() || $wp_query->is_page() ) {
$GLOBALS[more] = 1;
$GLOBALS[single] = 1;
}
if ( $wp_query->is_author() && isset( $wp_query->post ) ) {
$GLOBALS[authordata] = get_userdata( $wp_query->post->post_author );
}
}
The main ones are:
$GLOBALS[posts] = & $wp_query->posts;
$GLOBALS[post] = isset( $wp_query->post ) ? $ wp_query->post : null;
These two lines of code put the articles and the list of articles queried in the previous step into $GLOBALS. In the rendering phase, the article content will be read from $GLOBALS.
Rendering phase
The entry is a template-loader.php file. It will first determine which page is being requested. Parse_query() has been calculated in the data preparation phase. The core code is as follows:
$tag_templates = array( is_embed => get_embed_template, is_404 => get_404_template, is_search => get_search_template, is_front_page => get_front_page_template, is_home => get_home_template, is_privacy_policy => get_privacy_policy_template, is_post_type_archive => get_post_type_archive_template, is_tax => get_taxonomy_template, is_attachment => get_attachment_template, is_single => get_single_template, is_page => get_page_template, is_singular => get_singular_template, is_category => get_category_template, is_tag => get_tag_template, is_author => get_author_template, is_date => get_date_template, is_archive => get_archive_template, ); $template = false;
// Loop through each of the template conditionals, and find the appropriate template file.
foreach ( $tag_templates as $tag => $template_getter ) {
if ( call_user_func( $tag ) ) {
$template = call_user_func( $template_getter );
}
if ( $template ) {
if ( is_attachment === $tag ) {
remove_filter( the_content, prepend_attachment );
}
break;
}
}
if ( ! $template ) {
$template = get_index_template();
}
The key in the $tag_templates array is actually a callback function, which is all defined in wp includes/query.php. The actual method called is the corresponding method name in class-wp-query.php.
After calculating the current page, load the template file under the theme according to the currently configured theme.
In the template file, we can get the prepared data (data in $GLOBALS). Take the content of the article as an example,
In the template, call the_content() method, which is defined in post-template.php. The_content() calls get_the_content(), get_the_content() calls get_post() in post.php, and get_post() is defined as follows:
function get_post( $post = null, $output = OBJECT, $filter = raw ) { if ( empty( $post ) && isset( $GLOBALS[post] ) ) { $post = $GLOBALS[post]; }
if ( $post instanceof WP_Post ) {
$_post = $post;
}
//Other codes
return $_post;
}
The first line of the method uses $GLOBALS to retrieve the previously prepared article data.
After getting the article data, the get_the_content() method will assemble the article data, return it to the_content(), and the the_content() method will output it.
summary
So far, let's sort out the loading process of wordpress, hoping it will be helpful to you when you look at the source code. There are many details in the source code that need to be sorted out. When sorting out, we should pay attention to the use of global