I recently took a dive into wordpress, and I noticed something really unusual, When coding I noticed that a particular variable called the $post
variable was available for me to manipulate whenever I need it, as along as my page is within the wp-includes
, wp-themes
or wp-plugins
folder without me calling any external page or function.
So I started developing a site without wordpress hoping to understand the mystery behind that anomaly..
I would appreciate all help on making me understand this phenomenon. I would like to use such technique in building sites. Thanks…
That’s not an anomaly. That variable is present in global scope and is being defined in either of the files that you have mentioned. You can easily do it like
include.php
anyfile.php
And you can use it in your functions as well, as long as you refer to the global one, for example
anotherfile.php
Theory
Go through this PHP Doc once and you will have much better idea of how it all works.
Take a look at global variables:
http://php.net/manual/en/language.variables.scope.php
and superglobal as well:
http://www.php.net/manual/en/language.variables.superglobals.php
php.ini
register_globals = on
$post
$get
will be available anywhere