admin管理员组文章数量:1417670
I'm trying to understand, how WP makes its countless functions globally available in any theme or plugin file(without the need to manually require any file).
For instance, i've used my IDE to trace the deceleration of wp_enqueue_style, and it led me to some functions.wp-styles.php file. When searching for this file's name, i see that it's included once, in a file called script-loader.php.(which doesn't really help me further :D ).
Can someone give me a general explanation of how its done?
I'm trying to understand, how WP makes its countless functions globally available in any theme or plugin file(without the need to manually require any file).
For instance, i've used my IDE to trace the deceleration of wp_enqueue_style, and it led me to some functions.wp-styles.php file. When searching for this file's name, i see that it's included once, in a file called script-loader.php.(which doesn't really help me further :D ).
Can someone give me a general explanation of how its done?
Share Improve this question asked Aug 1, 2019 at 21:27 i.brodi.brod 1357 bronze badges2 Answers
Reset to default 2Themes and plugins are just files that are loaded alongside all the other WordPress files that contain its functions, and those are all loaded whenever WordPress is run. So there's no special trick or anything weird going on. WordPress just loads the files that contain these functions before it loads themes and plugins.
Keep in mind that when WordPress is loaded, it doesn't load your theme files directly. It loads index.php, which loads wp-blog-header.php which starts the process of loading everything else, ending with loading your theme. That's why you don't need to include anything into your theme/plugin.
Regarding your specific example, you just haven't gone far enough. /wp-includes/functions.wp-styles.php
is indeed loaded by wp-includes/script-loader.php
, but that file is loaded by /wp-settings.php
, which is loaded by /wp-config.php
, which is loaded by /wp-load.php
, which is loaded by /wp-blog-header.php
, which is loaded by /index.php
, which all requests are passed through via the .htaccess file.
The trick is the following: Just make use of WordPress' rich amount of hooks, filters and actions. That way, WordPress will (mostly) know what to do and sort things for you.
That may not be a satisfying answer in the first place or in terms of $GLOBALS.
However in general, the first thing to understand is the following:
Functions in a theme always go into the functions.php. This file is always loaded and will be the first place to start when building a theme and adding functionality to it. This file will contain all your code that manipulates the output and the things you want to do in the backend.
There, to add your own stuff all you need to do is to use one of the hooks, filters and/or actions.
See this link in the codex.
Same goes for a plugin. In case you want to add functionality not in a theme, but reusable - put it in a plugin. The basic setup may be tricky to understand, but once you get the hang of it, it's really cool.
You can also declare classes in your functions.php and call them somewhere in your theme. When building a plugin you need to follow the coding standards to make WordPress "see" your code.
Download, install and activate Hookr, visit your frontend. It will show all available "Hooks" where you can check in to inject your code.
本文标签: globalsHow does WordPress make its functions globally available
版权声明:本文标题:globals - How does WordPress make its functions globally available? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745277217a2651254.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论