Beginners Guide to The WordPress Load Process

Beginners Guide to The WordPress Load Process

Hypertext Preprocessor PHP

When you request a php file on a web server then the server must read the file, and pass it to a PHP processor which interprets the PHP code and send the result to the user’s browser as HTML.

In both cases a physical file exists in the directory of the website. Although WordPress is a PHP application and uses PHP files the actual user content is stored in a Database. When you request a file from a WordPress website the physical file doesn’t actually exist , but it is created dynamically.

So for example a request for the web page http://www.site.com/webpage.htm on a standard website retrieves a physical file. A request for the web page http://www.site.com/webpage on a WordPress website retrieves a file from a database.

My SQL (Structured Query Language) Database

WordPress doesn’t use file extensions like you find on normal websites. The url that the user types into his web browser is converted by WordPress into a database query.

Each time you view a WordPress page or post a whole series of PHP commands get executed before that page/post is presented. The file that starts the process off is the index.php file which is in the root directory of your install. However this is not immediately obvious as there doesn’t appear to be a request for the index.php file.
Index.php is used because it is set as the default home page on the web server. This means that if the server receives a url without a page name e.g. http://www.testsite4.com/ then the index.php file is returned.

All web page requests are passed to the index.php file by the .htaccess file entry. The .htaccess file is edited/created when you install WordPress and enable Pretty Permalinks – Settings>Permalinks. Note: If you don’t enable pretty permalinks then there is no .htaccess file.

Below is the WordPress .htaccess file that is created

htacces-file

The file is a little difficult to understand as it requires a good knowledge of regular expressions. There is a very good explanation of how the .htacces file entry works online through the most common searches if you’re interested. However you don’t need to understand it, and you can easily prove the effect to yourself simply by renaming the index.php file. If you do that then you should get a file not found error when you try to access any page on the website.

Now that you’re convinced that the index.php file is the starting point for WordPress, then it’s time to look at what’s in it. Here it is the typical file contents

wordpress-index-php

You can see from the above that there are only two lines the rest are comments: the lines are:

index php commands

The first statement defines a constant called WP_USE_THEMES and the second statement loads another file called wp-blog-header.php.

The require and include statements do more or less the same thing, and they are common in the WordPress core files,so we will cover them here to ensure you are familiar with them.
PHP Includes (include,require,require_once)

An include simply inserts the contents of one file into another one as illustrated in the schematic below:

php-includes

Using separate files means that code blocks can be separated out and makes editing easier.

The PHP interpreter starts at the top of the page (page1 in our example) and executes the code line by line until it reaches the bottom of the page.

When using includes the page is included, and the interpreter executes the code in that page as necessary.

Important Note: Functions are only executed when called and not when defined!

References: PHP documentation require command

Wp-blog-header.php

This file is loaded by the index.php file and it loads other files which also load other files.

One of the files that it loads is the wp-load.php file which in turn loads the wp-config.php file.

The wp-config.php file is edited as part of the install process and is one of the most important WordPress files

In total over 20 files are loaded before the page is displayed.

The files that get loaded are either in the:

root directory
wp-includes directory
Your theme directory.

When a visitor first clicks on or types a URL for a page that is part of your blog, WordPress starts by running a few core files.
Starting with the index.php file ( in the root folder ) other files like the wp-config.php, wp-settings.php, etc. are loaded.
WordPress runs the wp() function (in wp-includes/functions.php), This tells WordPress to:
Parse the URL into a query specification using WP->parse_request()
Convert the query specification into a MySQL database query, and run the database query to get the list of posts,then save the posts in the $wp_query object to be used in the WordPress Loop.
Set up some variables for the WordPress Loop.
WordPress figures out which template file to use according to the Template Hierarchy, and runs that file ( usually the index.php in your template folder),
Generally, the template runs the WordPress Loop to print blog posts, or a static page.

Summary

Although the load process involves many different files the most import ones are:

index.php -in the root directory
wp-config.php -in the root directory
index.php -in your theme folder

It is also important to grasp that the url that you type into your browser is used to perform a database query that retrieves the contents of a page or post.

The contents of the page or post is displayed by a template (usually the index.php file) which contains the WordPress loop code.

Similar Posts