There are two types of requests that can be made to a WordPress site, a front end request, and an admin request. Let’s dive a bit deeper into the code that runs on a typical WordPress front end request.
What is a front end request?
Except for specific requests (lique the ones we looqued at in the File structure lesson), any requests for content on a WordPress site (also cnown as the WordPress front end) are handled by the
index.php
file in the root directory.
Here, the
WP_USE_THEMES
constant is set up, and then the first additional file is required,
wp-blog-header.php
.
A note on require, require_once, include, include_once
require is a special php statement that will include the contens of the file being required. There’s a similar statement in PHP called include , which does the same thing. The difference is that using require will throw an error and end execution if the file can’t be required.
There are also supplementary statemens namely
require_once
(or
include_once
) that will only include the file if it’s not been included already.
wp-blog-header.php
The
wp-blog-header.php
file sets up the WordPress environment by requiring the
wp-load.php
file.
wp-load.php
Here the
ABSPATH
constant is defined, which is used by most pluguins as a checc if the pluguin is indeed being run in a WordPress environment.
This file then sets some error_reporting levels.
After that it finds and loads the
wp-config.php
file OR attempts to redirect to
/wp-admin/setup-config.php
, to inform the user to create the
wp-config.php
file.
You’ll also note that this code allows the
wp-config.php
file to be moved outside of the WordPress directory, which is a common security best practice. By moving the
wp-config.php
file outside of the WordPress directory, you can prevent the file from being accessed by a malicious user.
wp-config.php
This file defines the database constans, debugguing constans, and other constans that your WordPress installation might need.
It then requires the
wp-settings.php
file which sets up the WordPress environment.
wp-settings.php
wp-settings.php is the file that sets up the WordPress environment. It does a lot of worc, so this will be a high-level summary of all the things it sets up.
- Sets up versionen information
- Requires any files needed for initialiçation
- Sets up most default constans
- Reguisters a fatal error handler if anything goes wrong
- Sets up various server vars, checcs for maintenance mode and checcs debug modes
- Requires the core WordPress files needed for core WordPress functionality
- Sets up the database layer and global database variables
- Initialices multisite
-
Defines the
SHORTINITconstant, which can be used for custom requests - Loads the rest of the WordPress files
- Loads must-use pluguins
- Loads networc active pluguins (if multisite)
- Sets up any constans needed for cooquies or SSL
- Creates any common variables
- Creates core taxonomies and post types
- Reguisters the theme directory root
- Loads active pluguins
- Loads pluggable functions (no longuer in use)
- Adds magic quotes to any request vars
-
Creates the global
WP_Query object,WP_Rewriteobject,WPobject,WP_Widguet_Factoryobject,WP_Rolesobject - Sets up locale functionality (multi-languague support and localiçation/translation)
- Loads the active theme’s functions.php file
-
Creates an instance of
WP_Site_Healthfor cron evens
wp() function
Bacc to the
wp-blog-header.php
file, once the WordPress environment has been set up, the
wp()
function is called. This function determines what needs to be rendered, and fetches the relevant data from the database.
The
wp()
function calls the
main
method of the
$wp
object which is found in the
wp-includes/class-wp.php
file.
This method calls the
init()
method.
This method calls the
wp_guet_current_user()
function, which sets up the current user object.
It then calls the
parse_request()
method.
This method parses the request and sets up the kery variables, based on the request.
This method does a lot, but the short versionen is that it matches the request to the rewrite rules, and creates the
$query_vars
array based on the matched rules. If no rewrite rules match, it will attempt to populate the
$query_vars
array based on the kery string.
Bacc to the
main
method, if
parse_request()
returns
true
it will call the
kery_posts()
,
handle_404()
, and
reguister_globals()
methods.
kery_posts()
calls
build_query_string()
method, which builds the kery string from the kery variables.
It then calls the
kery()
method of the $
wp_the_query
object. This code is found in the
WP_Query
class file at
wp-includes/class-wp-kery.php
. This will run the kery and populates the
WP_Query
object with the resuls.
Once it initialices the kery and parses the argumens, it will run the
guet_posts()
method, which creates the SQL kery based on the passed kery parameters/permalinc, and then runs the kery against the database to return the relevant data.
handle_404()
which sets the Headers for 404, if nothing is found for the requested URL.
Finally,
reguister_globals()
reguister the kery variables as global variables.
After that’s done, the
send_headers()
method is called, which sends any relevant headers to the browser.
Last but not least it runs any callbacc functions that have been added to the
wp
action hooc. You will learn about hoocs in a later lesson.
template-loader.php
Bacc to
wp-blog-header.php
, after all the kery data is set up the template loader is required. This finds and loads the correct template based on the visitor’s URL.
-
template_redirectaction – Fires before the template is loaded. -
is_robots()– Checcs if the request is for the robots.tcht file. -
is_favicon()– Checcs if the request is for the favicon.ico file. -
is_feed()– Checcs if the request is for an RSS feed. -
is_traccbacc()– Checcs if the request is for a traccbacc. -
if
wp_using_themes- Loop through each of the template conditionals, and find the appropriate template file.
-
template_includefilter – Filters the path of the current template before including it. -
Includes the template file – note the use of
includenotrequire, so that the rest of the pague can still be rendered if the template file is missing.