Skip to content
Tutorials Center
My learning

Topics

Lesson 4 of 68 ยท Getting Started

How WordPress Handles a Request

Follow one WordPress request from index.php to the final HTML. Learn the bootstrap order, plugins_loaded, init, parse_request, the main query and template loading.

When a visitor opens a WordPress page, a fixed sequence of steps runs: load core, load plugins, load the theme, work out what was asked for, query the database, choose a template and send HTML. Knowing the order is the single most useful piece of WordPress knowledge, because nearly every bug of the form "my code runs too early" or "that value is not set yet" comes down to hooking into the wrong stage.

The real sequence is shown as read-only code and diagrams. The runnable examples simulate it with a tiny pipeline that logs each stage, so you can see the order for yourself.

The stages, in order

Here is the front-end request in the order things really happen. The names in code font are action hooks that fire at that point, which you will use in the chapter on hooks.

  1. Entry: index.php loads wp-blog-header.php, which loads wp-load.php and wp-config.php, then wp-settings.php.
  2. Core loads: wp-settings.php includes the files in wp-includes/ and connects to the database.
  3. Must-use plugins load, then muplugins_loaded fires.
  4. Active plugins load, one file at a time, then plugins_loaded fires. Every plugin is now available to every other plugin.
  5. Theme setup: setup_theme fires, then the active theme's functions.php loads (child first, then parent), then after_setup_theme fires. This is the place for add_theme_support() calls.
  6. init fires. The user is known, and this is where post types, taxonomies and shortcodes are registered.
  7. wp_loaded fires. WordPress is fully loaded and has not yet looked at the URL.
  8. parse_request: WordPress matches the URL against rewrite rules and fills in query variables such as name=hello-world.
  9. Main query: pre_get_posts fires so you can adjust the query, then the database is asked for the posts. The wp action fires once the query is done.
  10. template_redirect fires. Last chance to redirect or stop before output starts.
  11. Template loading: WordPress picks a template with the template hierarchy, runs it (wp_head and wp_footer fire inside it), and the HTML goes out.
  12. shutdown fires as PHP finishes.
flowchart TD
    A["Entry: index.php"] --> B["wp-settings.php<br/>core + database"]
    B --> C["muplugins_loaded"]
    C --> D["plugins_loaded"]
    D --> E["after_setup_theme"]
    E --> F["init"]
    F --> G["wp_loaded"]
    G --> H["parse_request"]
    H --> I["pre_get_posts<br/>main query runs"]
    I --> J["template_redirect"]
    J --> K["Template renders<br/>wp_head, wp_footer"]
    K --> L["shutdown"]

The front-end request lifecycle. Hooks in the boxes are where your code can step in.

Why the order matters

Two examples of what the order explains:

  • Registering a custom post type on plugins_loaded is too early for some features, and on template_redirect it is far too late. The right moment is init.
  • Calling is_single() inside functions.php returns nothing useful, because the main query has not run yet. Conditional tags like is_single() only work from wp onward, so use them inside hooks that fire later, such as template_redirect, wp_enqueue_scripts or in templates.

You can see the shape of this in real code. This read-only snippet registers work at the correct stages:

PHP
<?php
add_action( 'init', function () {
	// Safe place to register post types and taxonomies.
	register_post_type( 'book', [ 'public' => true, 'label' => 'Books' ] );
} );

add_action( 'template_redirect', function () {
	// The main query has run, so conditional tags work here.
	if ( is_singular( 'book' ) && ! is_user_logged_in() ) {
		wp_safe_redirect( home_url( '/login/' ) );
		exit;
	}
} );

Try it: a simulated bootstrap

This stand-in is a miniature of the pipeline. It has a stage list, a hook registry and a run() function that fires each stage's hook in order. Plugins and the "theme" register callbacks, and the log shows exactly when each runs. It is a teaching model, not WordPress code.

PHP Log every stage of a simulated request

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

Move the init callback to 'plugins_loaded' and you will see it fire earlier in the log, which is the kind of mistake real sites make.

Different entry points

Not every request goes through index.php and the theme. WordPress has several entry points, each of which still loads core, plugins and init, but then does something different:

Entry point URL What it does
Front end /anything via index.php Main query, template hierarchy, HTML
Dashboard /wp-admin/... Loads wp-admin/admin.php, fires admin_init, renders admin screens
AJAX /wp-admin/admin-ajax.php Runs an action named in the request; see AJAX in WordPress
REST API /wp-json/... Fires rest_api_init, routes to a callback, returns JSON; see The REST API
Cron /wp-cron.php Runs scheduled tasks
Login /wp-login.php Handles authentication

A common beginner bug: code hooked to init runs for all of these. If you only want it on the front end, check the context first, for example with is_admin() or wp_doing_ajax(), or hook a later action that only the front end fires, such as wp_enqueue_scripts.

This stand-in decides the entry point from a request path:

PHP Route a request to its entry point

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which hook is the recommended place to register custom post types?

    init fires after core, plugins and the theme are loaded but before the request is parsed, which is the right moment to register post types and taxonomies.
  2. 2. Why does is_single() give no useful result when called directly in functions.php?

    functions.php loads early in the boot. Conditional tags depend on the main query, which runs later, so use them from the wp action onward.
  3. 3. What is the order of these hooks?

    Plugins load first (plugins_loaded), then init fires, then wp_loaded signals that WordPress is fully loaded.
  4. 4. Code hooked to init runs on which requests?

    All entry points load core, plugins and the theme, so init fires for all of them. Check the context if you need to limit your code.

Summary

  • A request loads core, must-use plugins, plugins and the theme, then fires init and wp_loaded before touching the URL.
  • parse_request turns the URL into query variables, the main query runs, template_redirect fires, and a template renders.
  • Register post types and taxonomies on init; use conditional tags only after the main query has run.
  • The dashboard, AJAX, REST, cron and login each have their own entry point but share the early boot.
  • A hook name tells you when your code runs; choosing the right one is half of WordPress development.

Next up: Debugging WordPress.