Search

Behind the Scenes Peek at How WordPress is Loaded: Part I

Listen to this article

install-wordpress-ubuntuI 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!

It’s magic.

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:

  1. The WordPress Directory Structure
  2. The WordPress Load Process
  3. The WordPress Template Hierarchy

[space]

The WordPress Directory Structure

Once you install WordPress, whether locally on LAMP or on a server, you’ll notice several files added.

  1. You have files in the ‘wordpress’ directory (or public_html directory, or the folder where you’ve added the files)
  2. 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.

[space]

wp-admin

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.

[space]

wp-content

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.

[space]

wp-includes

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.

[su_note note_color=”#fffdb0″ text_color=”#000000″]Quick tip: The way the directories are structured and the files are segregated can teach you how to structure your files for a plugin you build.[/su_note]

[space]

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.

[space]

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.

index.php

You’ll notice that index.php does two things:

  1. Defines a constant WP_USE_THEMES
  2. 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.

[space]

wp-blog-header.php

If you take a look at this file. You’ll notice it does three things:

  1. Includes wp-load.php
  2. Calls wp()
  3. 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.

[space]

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).

[su_note note_color=”#fffdb0″ text_color=”#000000″]A little piece of trivia: To load the config file it searches either in the same path or a level above. Sometimes the wp-config file is placed in a directory above the ‘wordpress’ directory level for security purposes.[/su_note]

[space]

wp-config.php

You’re probably familiar with this file already, since you had to involve it while installing WordPress. This file contains the following:

  1. The database settings- the database name, username, password,
  2. The Auth Keys- automatically generated and used for cookies and authentication
  3. The database table prefix
  4. The WP_DEBUG constant to turn on debugging mode
  5. The wp-settings.php

The wp-settings.php is where all the magic happens. 🙂

[space]

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!

Namrata

Namrata

Leave a Reply

Your email address will not be published. Required fields are marked *

Get The Latest Updates

Subscribe to our Newsletter

A key to unlock the world of open-source. We promise not to spam your inbox.

Suggested Reads

Join our 55,000+ Subscribers

    The Wisdm Digest delivers all the latest news, and resources from the world of open-source businesses to your inbox.

    Suggested Reads