0

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?

1

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

  • Yes, i played a little bit with some PHP sandbox and realized that this is how the language works. Simpler than i thought :D – i.brod Aug 2 at 7:47
0

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.

  • I think you misunderstood my question :D What i asked is how are WP's various functions made available in my theme/plugin, not how to use them :D . Also, i wasn't talking about my own created functions(like those placed inside functions.php), but those that come from WP's core. – i.brod Aug 1 at 22:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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