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.
This video below shows how home pages work.
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
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 at stackexchange if you are 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.
The Index.php file
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 with the important parts highlighted:
You can see from the above that there are only two lines the rest are comments: the lines are:
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:
Instead of putting all of the WordPress code in a single file it uses separate files organised into code blocks, which 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
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.
This article gives a more detailed overview of the files that get loaded, and what they do.
Here is an abridged description of the load process taken from the WordPress Codex.
- 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.
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.
–> Goto WordPress Templates Explained.
- How WordPress Works
- Understanding the WordPress Query-Beginners Guide
- WordPress File And Directory Structure Explained
- PHP Callbacks and Callback Functions
- Beginners Guide to WordPress Templates
- Beginners Guide to the WordPress Loop
- Understanding and Editing the wp-config.php File