WordPress source code analysis - main process analysis, wordpress architecture interpretation

14 minutes
seven hundred and eighty-one
zero

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

This article is written by: Chief Editor Published on Software Development of Little Turkey , please indicate the source for reprinting: //hongchengtech.cn/blog/1439.html
Kuke_WP editor
author

Related recommendations

1 year ago (2024-02-20)

Industry Fit! Preferred element of WMS warehouse management system, wms warehouse software

Enterprise managers often think that warehouses are inefficient, high cost places, and belong to heavy asset operations. With the development of enterprise business, if the warehouse needs to be expanded in traditional ways, the cost is relatively high. At the same time, it also faces problems such as lack of operating experience. In the operation link, the process of warehouse, allocation, human resource matching and management is very complicated, and the team's professional ability is also highly required
seven hundred and eighty-three
zero
1 year ago (2024-02-19)

Supply chain billing system management (I): system overview, what are the supply chain management fees

In recent years, with the continuous development of e-commerce industry and increasing business, everyone has started to distribute goods online, and the supply chain billing system needs to manage more and more things. How to manage the billing system? The author summarizes some contents about settlement based on his own practical experience, hoping to enlighten you. After working on the warehouse management system for several years, I was transferred to work as a supplier
five hundred and fifty-six
zero
1 year ago (2024-02-19)

Multi merchant system management - store background design, what is the meaning of multi merchant classification

Simply understood, multi merchants are a large mall. The platform can manage merchants who settle in the mall. The merchants who settle in the mall have independent backstage. They can log in and add goods to the shelves by themselves, manage stores by themselves and other information functions. Then how to design the backstage of the store? Let's see the author's sharing. I hope it can help you. 1、 Introduction The backstage of the store is an important part of the e-commerce platform
six hundred and forty-eight
zero
1 year ago (2024-02-19)

Jiangyang District of Luzhou City took the lead in the city's full coverage training on domestic waste classification management regulations, Luzhou waste treatment

Source: Original Draft On January 10, the People's Congress of Jiangyang District, Luzhou City and the District Government jointly carried out a training on the regulations of the Regulations on the Classified Management of Domestic Waste in Luzhou City (the Regulations for short), and invited Lei Zhengyun, the chairman of the Legislative Affairs Committee of the Municipal People's Congress, to give a live lecture, so as to guide the comprehensive and systematic grasp of the contents and legal functions and responsibilities of the Regulations, deeply understand the specific specifications of the Regulations, and quickly set off
three hundred and seventeen
zero
1 year ago (2024-02-19)

Simeng CMS (smcms) content management system, Simeng Central Primary School

SMCMS (Simon CMS) is a content management system developed based on the microbee http rapid development framework. Product development follows the concept of simplicity, security, high concurrency and efficiency. Enterprise level web content management software for high-end users is designed to help users solve the increasingly complex and important web content creation, maintenance, publishing and response
three hundred and sixty-one
zero
1 year ago (2024-02-19)

Does the website have to install a content management system?, What apps are needed to install software on the website

1: The role of the website is to let companies or enterprises display their own windows, but also to let more customers or potential customers know their work and products. Through the website, customers can understand their products and services more intuitively, and can also provide more services to meet customer needs. 2: The role of the content management system The content management system can help
four hundred and fifty-six
zero

comment

0 people have participated in the review

Scan code to add WeChat

contact us

WeChat: Kuzhuti
Online consultation: