I understand that after installing WordPress, you want to jump right into developing a theme or a plugin. Which is exactly what we’ll be learning next. But before we get there, we need to understand how WordPress works- how the core is initialized. What happens behind the scenes when a page request is made to the page actually being displayed? How does WordPress make all of this happen!
Sure it’s something to do with index.php and wp-config, and there are template files involved. But there’s a lot more to it. LOT more. Something that I can’t possibly cover in this post. And you wouldn’t need all the details right away. What you need to understand are the basics of WordPress initialization and the load process.
Here’s what we’ll be learning:
- The WordPress Directory Structure
- The WordPress Load Process
- The WordPress Template Hierarchy
The WordPress Directory Structure
Once you install WordPress, whether locally on LAMP or on a server, you’ll notice several files added.
- You have files in the ‘wordpress’ directory (or public_html directory, or the folder where you’ve added the files)
- wp-admin, wp-includes, wp-content directories created.
I’ll be getting to the files in the ‘wordpress’ directory in a while. But let me tell you about other directories created.
The wp-admin directory contains files related to content being displayed in your WordPress admin panel- or the backend of your site as you would prefer to call it. So here you’ll find files such as dashboard.php (in includes), options.php, plugins.php, widgets.php, users.php and so on.
In wp-content, you’ll find your plugin files, theme files, uploads. You’ll be familiarized with this folder when building a plugin or theme.
Here is where WordPress core resides. WordPress’ default functionality, default hooks, shortcodes can be traced back to files in this folder. For example you have wp-db.php, functions.php, the WP class, and so on.
All these files along with the files in the ‘wordpress’ folder- work towards displaying a page queried. Let’s take a look at the order in which these files get accessed.
The WordPress Load Process
As you would know, the process begins with index.php. This file is present in the ‘wordpress’ directory. So if you want to know exactly how WordPress is loaded, you have to take a look at this file. And by look, I mean you need to actually go ahead and actually open this file.
You’ll notice that index.php does two things:
- Defines a constant WP_USE_THEMES
- Includes wp-blog-header.php
The constant WP_USE_THEMES is by default set to true. This constant tells WordPress to use a theme and use the template hierarchy to display your page content. You wouldn’t necessarily need to set this to false. But in cases where you’d want to use just the admin part of WordPress, or use a different frontend, you could set this variable to false.
The wp-blog-header.php continues the load process.
If you take a look at this file. You’ll notice it does three things:
- Includes wp-load.php
- Calls wp()
- Includes template-loader.php
The wp() function initialises the WP class object. And the template-loader.php (present in wp-includes) initialises page templates. But before all of this happens, WordPress is loaded by wp-load.php.
You’ll notice the ABSPATH constant being defined in this file. This constant can be used in plugin or theme development to reference the path of the root directory.
Next, the wp-load.php loads the wp-config file. If the wp-config.php file does not exist, you’ll be prompted to create one (like you had to while installing WordPress).
You’re probably familiar with this file already, since you had to involve it while installing WordPress. This file contains the following:
- The database settings- the database name, username, password,
- The Auth Keys- automatically generated and used for cookies and authentication
- The database table prefix
- The WP_DEBUG constant to turn on debugging mode
- The wp-settings.php
The wp-settings.php is where all the magic happens. 🙂
Exploring the wp-settings.php is quite an elaborate topic. Here is where the core WordPress files are loaded. To avoid information overload, we’ll take a look at it in the next section- Part II: Behind the Scenes Peek at How WordPress is Loaded. In Part II, we’ll also take a look at The WordPress Template Hierarchy, which finally completes the page display process.
If you have any questions, for the content shared uptil now, do feel free to leave your questions in the comment section below!