plugins - How do action and filter hooks understand where to look for the core function that we hooked our function to them

admin2025-06-04  5

I want to know how does WordPress core files handle the relationship between module and core of the WordPress? We all know it uses add_action() and add_filter() to manipulate data according to our will in our plugin and there is a $tag parameter that is the function name that we want to hook our action and filter to core function of the WordPress. But how does the action and filter know where is the location of the $tag parameter (function) is in the WordPress core?

I start to the reading the WordPress file and yet I have didn't find my answer.

I want to know how does WordPress core files handle the relationship between module and core of the WordPress? We all know it uses add_action() and add_filter() to manipulate data according to our will in our plugin and there is a $tag parameter that is the function name that we want to hook our action and filter to core function of the WordPress. But how does the action and filter know where is the location of the $tag parameter (function) is in the WordPress core?

I start to the reading the WordPress file and yet I have didn't find my answer.

Share Improve this question edited Jan 2, 2019 at 21:57 butlerblog 5,1413 gold badges28 silver badges44 bronze badges asked Jan 2, 2019 at 20:18 MahdiMahdi 292 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 1

I think you may be looking at this the wrong way. It doesn't need to look for the $tag function anywhere in core (or anywhere else for that matter). That function needs to already be loaded in the current sequence. As long as whatever file that contains your custom function is loaded at the time the action/filter is triggered, then your function can run (because it is in memory at that time) - otherwise, you'll get an error since that function will be undefined.

Here's a practical discussion of what I'm talking about. If you apply something in your theme's functions.php file that is hooked to plugins_loaded, your function will never fire (even though it's right there in functions.php) because the action has already passed by the time the functions.php file is loaded. You need a later action.

Or another example would that you need to make sure the file containing your function is actually loaded so that when your action or filter call is triggered that the file containing the function is loaded into memory. For example, if you have developed a plugin that consists of multiple files and in the main plugin file that gets loaded has a filter that triggers "my_cool_filter_function()" but you never bothered to include() or require() that file in your sequence, then the function will throw an undefined error because the function does not exist when the filter is triggered. However, as long as you've run include() (or require()) to load that file, then you'll be fine because my_cool_filter_function() will be available to be run.

Function names have to be unique in PHP, so if some function is assigned to given hook, then there is nothing to look for... There can be only one function with such name - or you’ll get fatal error...

I read the whole core files in wordpress. And it is split multiple file across the core folders actually it is very smart of them to do such a thing

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749042348a315865.html

最新回复(0)