1

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.

3 Answers 3

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.

2
  • no I mean how does add action know what is the $tag function is?
    – Mahdi
    Jan 3, 2019 at 4:24
  • In your original question you said you how does it know "where." Now your saying you mean how does it know "what" the function is? The answer to that is whatever value you assign to $tag. $tag must be the value of a named function that is currently loaded (as described in my answer).
    – butlerblog
    Jan 3, 2019 at 17:10
0

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...

0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.