Skip to main content
We are Brand SEO Beijing serving international business, your marketing partner, Contact us by mi@mgsh.com.cn

Website source code research - foreground running process Hook

As mentioned earlier, all the processing of the WP website is in index.php, with a single entry.And there are two steps here.One is the preparation of data, and the second is the loading of templates.So where is our plugin call? ?It can be said that the data required by the plug-in is prepared in the first step, the calling mechanism of the plug-in is also initialized in the first step, and some interface calls are also made in the first step.Then the second part is mainly to call the method of the interface in a specific place.
In fact, the so-called plug-in, to put it bluntly, is a bunch of functions. The author of the plug-in hopes that this bunch of functions can be inserted and run in some places in the WP running process.For example, we want a keyword to filter something, every time you encounter a "grass mud horse", you replace it with "***".Then you can write a plug-in that executes a function before entering the database every time the log is submitted, so that you can replace the "grass mud horse" in this $content with "***".This is the plugin.
Then why don't we just change the source code directly, it's open source anyway.I think there are two reasons for this problem.
1. Open source is right, but I need to look at all the source code for a small function, is it a little bit. . .
2. You change the source code and you use it, and others will use you to teach him how to change it.Is it a bit. . .And if the other party doesn't understand the program, OMG.
So it's better to write it as one thing, the kind that fools can use.This is where the plugin appears.
Maybe you still think, then I will write an execution script and let it add some code to the specified line of the specified file, isn't it also a "plug-in"?
Yes, that will definitely work.However, if other people have other plug-ins, and everyone has to change the files, is it possible that conflicts may arise?
It doesn't matter if you don't understand the reason for using the plugin above.The following is the specific implementation of the plug-in.
Plugins are implemented in WP with a mechanism called hooks, hooks: hooks are also.That is to put some hooks in designated places, and then you can write your code as functions, hang on some hooks, and when the program executes to the hooks, execute the above series of functions.
When did this hangup happen?How did it happen?
When we execute the wp-settiongs.php mentioned above, it contains a bootstrap file (that is, a PHP file) for each of our plugins, and some methods are called in this file, such as: add_action("hook", "function").For example, apply_filter("hook", "function").. Of course, there are also a series of functions to delete and modify.You can open the outer PHP file of a plugin and look at the code inside.
What do these codes mean.They actually describe a corresponding relationship, that is, the corresponding relationship between a hook and some functions, and the hook function above is called each time.It will make this hook associated with one more function.In fact, all this information is stored in the associative array of PHP.You can think about how it is saved, I will not say it.Time to look at the code.Ha ha.
Then such a global variable is initialized, and when some hooks are specifically executed later, the function is executed.Specifically this is achieved.Not amazing is it.It just really works.
The bad part is that this initialization is done every time you do something, whether you need it or not.He will be ready.
That's why WP is so slow.Interested friends can try var_dump PHP's $GLOBALS, the amount of data is quite astonishing, if the machine is not very good, it may cause your browser to freeze.
Source of this article: www.cnblogs.com/vicenteforever/category/224217.html

Back to Top