当前位置:网站首页>Simple analysis of WordPress architecture
Simple analysis of WordPress architecture
2022-06-24 21:11:00 【The smell of tobacco】
Preface
Recently, I was building my own blog site , I chose the one that uses more websites WordPress, With slow use , I was impressed by its flexible plug-ins and themes . Basically anything you want to do , Can be added in the form of plug-ins . Whether it is the cache before access 、 Statistics after access 、 Filtering in access 、 Modification of various processes, etc , Almost all of them can be modified in the form of plug-ins . I think it's cool , If I can write the architecture like this in my ordinary business , What other changes in demand can embarrass me ?
For this reason , I am right. WordPress A simple analysis is made , This is the benefit of open source . I started from index.php The file tracks the beginning and end of the entire request step by step . Because of limited ability , This is probably the most stupid way .
analysis
Execute the process
index.php The document is simple , Just one sentence :
require __DIR__ . '/wp-blog-header.php';
and wp-blog-header.php What about the documents , It's also very simple. :
if ( ! isset( $wp_did_header ) ) {
$wp_did_header = true;
require_once __DIR__ . '/wp-load.php';
wp();
require_once ABSPATH . WPINC . '/template-loader.php';
}
And this , Have already put WordPress The implementation process of .
1. Prevent duplicate loading
! isset( $wp_did_header ) Judge , To prevent the file from being loaded repeatedly , Just skip
2. load library / The theme / plug-in unit
The second step introduces wp-load.php file , Then introduced wp-config.php file , And then we introduced wp-settings.php file , The actual loading process , It's just wp-settings.php In file . This file does the following
- Import initialization file
- Constant definition
- Import and stock in
- Add plug-ins
- Load theme
Come here , There is no query for the current page data , Only the initialization process has been completed .
3. Query page data
wp() Function is a method to perform page data loading , According to the current page , Query the data to be displayed in the database , Prepare the data to be displayed .
4. The page display
Finally introduced template-loader.php file , Its function is to display the data visually .
5. complete
thus , The display process of the whole page is over . Follow this step to see , The whole process is quite clear .
But I still haven't answered the first question , Where is its flexibility ? The above simply describes the overall loading process , But the details have not been mentioned yet .
The page display
WordPress Where the page is loaded , It's the last one template-loader.php This document .

It is based on the current page , Load different files for presentation . As for why the page is so flexible , Just look for a page and you will know . index.php:

Jigsaw puzzle generation page . It can be customized for each location , And assemble it . So each topic has a high degree of flexibility , You can set up your own page , You can also choose to discard some content without showing it .
in addition , HTML When loading the page , Several templates will be searched , If you are visiting : How does the computer synchronize time At the time of this article , get_single_template Method will find the following files in turn :
single-post- How does the computer synchronize time .phpsingle-post-%e8%ae%a1%e7%ae%97%e6%9c%ba%e6%98%af%e5%a6%82%e4%bd%95%e8%bf%9b%e8%a1%8c%e6%97%b6%e9%97%b4%e5%90%8c%e6%ad%a5%e7%9a%84.phpsingle-post.phpsingle.php
If a file exists , It will load directly . Have you realized anything . This thing can be used as a cache . however , sorry , Before doing this , The query data has already been checked , So the cache is not added , Not much use for eggs. .
Hook function
If WordPress Just able to assemble pages in a jigsaw puzzle way , That's not flexible enough , Because only the page can be operated , Without affecting the execution process . Impact on execution process , Is its various hook functions . WordPress The hook function of do_action and apply_filters Two methods are called ,
Read the method add_action Find out , It is simply called add_filter Method . In other words, the two methods are the same internally . Personal understanding , do_action Pay attention to the insertion of process , Add a piece of logic to the main process , no return value . and apply_filters Method has a return value , Pay more attention to data processing .
stay WordPress in , Various hook calls can be seen everywhere , When initializing 、 Add plug-ins 、 The plug-in loading is complete 、 Loading themes, etc .
for instance , There is a caching plug-in , By adding init Hook function , Put the page content echo after , Direct execution die function , To achieve the effect of rapid return .
But in the process of checking the source code , There is a question , All hook function calls , Are called directly using strings , Such as do_action('init'). This generic variable , Shouldn't I write a list of constants ?
Fortunately, the official maintained a list of hook functions , Lists all the hooks , At the same time, it explains and points out the specific address of the call . You can have a look when you need it . I counted it , At present, a total of 1470 Hook . https://developer.wordpress.org/reference/hooks/
so to speak , WordPress Through various hooks and jigsaw puzzle pages , Realize the personalized customization of display and process respectively . And this hook function is nothing new , Interface monitor 、 Various beforeActionafterAction wait , It is often used in the ordinary development process . I just didn't use this extreme .
Other details
Configuration is loaded
WordPress The configuration of is stored in MySQL Medium , The way to request to load the configuration file is to execute sql Inquire about :
SELECT option_name, option_value FROM $wpdb->options WHERE autoload = 'yes'
Directly configure all the configurations in the table , Read it at one time , and , There are still a lot of data , Give you an intuitive feeling , I saved the results to txt file , file size 1.4mb.
If this query can increase the cache , Or through the configuration file , It can save some consumption . however , If you want to modify the configuration through plug-ins , sorry , This can't be . because The first read of the configuration is when wp_not_installed() Function time , The plug-in has not been loaded yet . If you want to change it , It seems that we can only modify the source code ,
When loading the configuration , First read in the request cache :

Therefore, the configuration can be put into the request cache in advance . Calling method wp_start_object_cache() After loading the cache , Immediately called wp_cache_add( 'alloptions', $alloptions, 'options' ); Method , Global configurations can be pre cached , It's an experiment , It works . If you pursue the ultimate performance , You can consider .
Configure storage
See the database configuration table wp_options When the value of the plug-in is enabled in , I have no idea , The stored content is like this :
a:7:{i:0;s:49:"easy-table-of-contents/easy-table-of-contents.php";i:1;s:47:"simple-yearly-archive/simple-yearly-archive.php";i:2;s:30:"wp-githuber-md/githuber-md.php";i:3;s:29:"wp-mail-smtp/wp_mail_smtp.php";i:5;s:27:"wp-super-cache/wp-cache.php";i:6;s:31:"wpdiscuz/class.WpdiscuzCore.php";i:7;s:32:"xml-sitemap-feed/xml-sitemap.php";}
This is this. , What is this? ? Look not to understand , But it seems that I can understand . So I tracked the resolution of this value , This is the following function :

The parsed data is :
{
"0": "easy-table-of-contents/easy-table-of-contents.php",
"1": "simple-yearly-archive/simple-yearly-archive.php",
"2": "wp-githuber-md/githuber-md.php",
"3": "wp-mail-smtp/wp_mail_smtp.php",
"5": "wp-super-cache/wp-cache.php",
"6": "wpdiscuz/class.WpdiscuzCore.php",
"7": "xml-sitemap-feed/xml-sitemap.php"
}
Is it easy to understand ? What is stored is through serialize Function to serialize the object , therefore , Ask weakly , Direct deposit json String is not good ?
Global variable definition
stay WordPress Global variables are everywhere in the . When I look at the cache file , You see this code :

But it's strange , I search variables globally $wp_object_cache, But I can't find the definition . Finally, I found its definition bit by bit .

And this functional style is everywhere , If you want to find out where a variable is used , It's hard to find . and , Direct reference to global variables , It also makes it difficult to modify variables later . We can see such a living example in the source code :

This style leads to a consequence , Once a variable is defined , I can't take it off .
Database query records
When viewing database queries , See this code :

in other words , If you define SAVEQUERIES Constant , And for true, Then the query will be sql recorded . stay log_query In the method , Recorded queries variable .
This operation is quite convenient for database tuning . Define constants in the configuration file , In the end get all sql And execution time
summary
For this kind of content full of global variables and hook functions , I am tired of reading , I often look at it and lose it . But I still found many interesting places .
I wanted to see why it is so flexible , It turns out that it has been used in the ordinary development process , however WordPress The handling of some content still gives me some inspiration .
For example, this kind of puzzle page composition , You can separate page presentation from data processing . When developing interfaces , Is it possible to draw on similar ideas . There's a problem with this approach , Even if the page does not use the data , It is also found in the query , In the case of performance oriented interfaces , It must be unbearable . Or you can ask the exhibitor to configure the data you need to use ? But in that case , The coupling is high again , Flexibility has also decreased , Difficult to engage in .
But the most important thing is , This is what I am using now , I don't know how to do it . If there is customization demand in the future , We won't have no way to start .
边栏推荐
- Sleep revolution - find the right length of rest
- How to apply agile development ideas to other work
- Packaging_ Conversion between basic type and string type
- Builder mode -- Master asked me to refine pills
- 2021-09-30
- What are the problems with traditional IO? Why is zero copy introduced?
- Shrimp skin test surface treated
- The AI for emotion recognition was "harbouring evil intentions", and Microsoft decided to block it!
- Undo log and redo log must be clear this time
- Use the transparent [x] cross button image in the dialog
猜你喜欢

JMeter response assertion
![[普通物理] 光栅衍射](/img/f3/965ff7cd3bb76b4f71b69b9d12ece3.png)
[普通物理] 光栅衍射

Summary of idea practical skills: how to rename a project or module to completely solve all the problems you encounter that do not work. It is suggested that the five-star collection be your daughter

Create a multithreaded thread class

Sleep revolution - find the right length of rest

Bean lifecycle flowchart

Curl command

Intermediary model -- collaboration among departments

What does virtualization mean? What technologies are included? What is the difference with private cloud?

微信小程序中使用vant组件
随机推荐
Nifi fast authentication configuration
微信小程序中使用vant组件
Prototype mode -- clone monster Army
The Google File System (GFS) learning notes
Bean lifecycle flowchart
Summary of idea practical skills: how to rename a project or module to completely solve all the problems you encounter that do not work. It is suggested that the five-star collection be your daughter
Sequence stack version 1.0
How Fiddler works
Why do we always "give up halfway"?
After screwing the screws in the factory for two years, I earned more than 10000 yuan a month by "testing" and counterattacked
Freshman girls' nonsense programming is popular! Those who understand programming are tied with Q after reading
Adding subscribers to a list using mailchimp's API V3
Haitai Advanced Technology | application of privacy computing technology in medical data protection
C langage pour le déminage (version simplifiée)
database/sql
虚拟化是什么意思?包含哪些技术?与私有云有什么区别?
opds sql组件能不能将流程参数通过上下文传给下一个组件
Several common command operations in win system
An example illustrates restful API
等保备案是等保测评吗?两者是什么关系?