admin管理员组文章数量:1130349
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 badges3 Answers
Reset to default 1I 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
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 badges3 Answers
Reset to default 1I 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
本文标签:
版权声明:本文标题:plugins - How do action and filter hooks understand where to look for the core function that we hooked our function to them 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749042320a2307318.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论