A developer’s life beyond the screen.


3. Understanding the WordPress Request Lifecycle

Before you start writing plugins or building custom features, it’s important to understand how WordPress loads a page.
Knowing the flow helps you understand where your code should run and why certain hooks exist.
This concept is simple once you see it step by step.


What Happens When Someone Opens a WordPress Page

When a user visits a WordPress site, a lot of things happen in the background.
Here’s a simple explanation:

  1. The request comes to the server
  2. WordPress loads its core files
  3. It loads active plugins
  4. It loads the active theme
  5. It decides which template to show
  6. Your page finally displays

Each of these steps triggers certain “hooks” (actions and filters).
You will use these hooks to run your custom code.


Breaking Down the Loading Process

Let’s look at this process in a little more detail — but still beginner friendly.

1. WordPress Starts Booting

Everything begins from a file named index.php in the WordPress root folder.
This file doesn’t do much. It simply tells WordPress to start loading the system.

2. Core Files Are Loaded

Files like wp-load.php, wp-settings.php, and others prepare the WordPress engine.
At this stage:

  • PHP sessions may start
  • Constants are defined
  • Some global functions become available

You don’t usually modify anything here, but it helps to know this step exists.

3. Plugins Are Loaded

WordPress loads all active plugins in a specific order.
This is where many developers add their logic.

Important hooks that run during this time:

  • muplugins_loaded
  • plugins_loaded

You will use plugins_loaded often, because it’s one of the earliest safe places to load your plugin’s classes or functions.

4. Themes Load After Plugins

After plugins are ready, WordPress loads:

  • The active theme
  • The theme’s functions.php
  • Template files (based on the page being viewed)

This happens later in the flow.
Hooks like after_setup_theme and init occur here.

5. WordPress Decides What to Show

WordPress checks:
“Is the user viewing a post? A page? A category? Search results?”

Based on this, it selects the right template file using the template hierarchy.

For example:

  • Single post → single.php
  • Page → page.php
  • Category → category.php
  • Fallback → index.php

If you’re building themes, this is where your template files matter.

6. The Final Output Is Displayed

Once everything is loaded, the page is rendered in the browser.

Your plugin or theme code may also run here, such as:

  • Adding scripts
  • Modifying content
  • Displaying custom UI

Hooks like wp_head and wp_footer run at this stage.


Why This Flow Is Important for Developers

Understanding this flow helps you know where to attach your code.

Here are a few examples:

  • If you want to register custom post types → use init
  • If you want to load plugin classes early → use plugins_loaded
  • If you want to add theme supports → use after_setup_theme
  • If you want to enqueue scripts → use wp_enqueue_scripts

Most beginners struggle because they call functions at the wrong stage.
Once you understand the lifecycle, these problems disappear.


Simple Example of Using a Hook

Here’s a clean example of hooking into init:

add_action('init', function () {
    register_post_type('movie', [
        'label' => 'Movies',
        'public' => true,
    ]);
});

The init hook is the right place for this because WordPress is fully loaded and ready, but templates are not yet being displayed.


You Don’t Need to Memorize Everything

Don’t worry if you can’t remember the entire lifecycle immediately.
As you write more plugins and themes, the flow will start making sense naturally.

Just remember these key points:

  • Plugins load before themes
  • plugins_loaded is early
  • init is a common hook for setup
  • Templates load near the end

That’s enough for now.


What’s Next

In the next article, we’ll look at how a WordPress plugin is structured.
You’ll learn:

  • How to organize files
  • What goes inside the main plugin file
  • How to load classes
  • How to avoid messy code
  • How to prepare a clean base for real projects