Skip to content

Instantly share code, notes, and snippets.

@johnbillion
Last active February 2, 2024 20:06
Show Gist options
  • Save johnbillion/4fa3c4228a8bb53cc71d to your computer and use it in GitHub Desktop.
Save johnbillion/4fa3c4228a8bb53cc71d to your computer and use it in GitHub Desktop.
WordPress Flow
INITIALISATION
==============
load wp-config.php
set up default constants
load wp-content/advanced-cache.php if it exists
load wp-content/db.php if it exists
connect to mysql, select db
load object cache (object-cache.php if it exists, or wp-include/cache.php if not)
load wp-content/sunrise.php if it exists (multisite only)
load l10n library
load mu plugins
DO_ACTION 'muplugins_loaded' (only accessible to mu plugins)
load active plugins
load pluggables.php
DO_ACTION 'plugins_loaded' (first hook available to plugins)
load rewrite rules
instantiate $wp_query, $wp_rewrite and $wp.
$wp_query is a global instance of the WP_Query class. For more info, see ANY QUERY
$wp_rewrite is a global instance of the WP_Rewrite class and contains our rewrite rules and functions
$wp is a global instance of the WP class and contains the functions that will parse our request and perform the main query (see REQUEST)
DO_ACTION 'setup_theme'
include child theme functions.php
include parent theme functions.php
DO_ACTION 'after_setup_theme' (first hook available to themes)
set up current user object
DO_ACTION 'init'
register widgets (DO_ACTION 'widget_init')
call wp() (which calls $wp->main())
REQUEST
=======
$wp->parse_request()
loop over rewrite rules to find a match
APPLY_FILTERS 'query_vars' to the publicly available query vars
fill query vars with $_POSTs, $_GETs, and rewritten vars
APPLY_FILTERS 'request' to the request variables
DO_ACTION_REF_ARRAY 'parse_request' with array of request vars (query vars, request, matched rewrite rules, etc)
DO_ACTION_REF_ARRAY 'send_headers' with the 'WP' object.
THE MAIN QUERY
==============
$wp->query_posts()
goto ANY QUERY
if posts are empty, set is_404() (and send 404 headers)
set all the query_vars to global variables
DO_ACTION_REF_ARRAY 'wp' with the main WP object
ANY QUERY
=========
WP_Query->query( query vars )
WP_Query->parse_query( query vars )
build query parameters based off query vars
set WP_Query->is_* vars based off query parameters
if this query is $wp_the_query then these determine the values of the global is_*() functions too
DO_ACTION_REF_ARRAY 'parse_query' with WP_Query object (query parameters, query vars, conditionals)
WP_Query->get_posts()
DO_ACTION_REF_ARRAY 'pre_get_posts' with WP_Query object
APPLY_FILTERS_REF_ARRAY 'posts_search' with search SQL
series of APPLY_FILTERS on the query SQL (if suppress_filters=false):
* posts_where
* posts_join
* posts_where_paged
* posts_groupby
* posts_join_paged
* posts_orderby
* posts_distinct
* post_limits
* posts_fields
* posts_clauses
APPLY_FILTERS_REF_ARRAY 'posts_request' (if suppress_filters=false)
fetch posts from the database
APPLY_FILTERS_REF_ARRAY 'posts_results' (if suppress_filters=false)
prepend sticky posts
APPLY_FILTERS_REF_ARRAY 'the_posts' (if suppress_filters=false)
return posts
TEMPLATE
========
DO_ACTION 'template_redirect'
if is_feed()
load the feed template
else
look for template file in theme based on template hierarchy
APPLY_FILTERS 'template_include'
load the template file (which usually runs a loop @TODO document a loop)
DO_ACTION 'shutdown'
@ahmadawais
Copy link

Thanks so much! ★

@MQuigg
Copy link

MQuigg commented Feb 15, 2020

Thanks, this is excellent! Has this changed any over the past few years?

@johnbillion
Copy link
Author

@MQuigg it's quite out of date, but generally still accurate

@lkraav
Copy link

lkraav commented Nov 25, 2021

Landed from https://twitter.com/wpmark/status/1463817518565445633

Very nice @johnbillion do you think we could continously auto-generate this from source code?

@johnbillion
Copy link
Author

To be honest I don't think so. The intention isn't to cover every action that fires and every file that loads, just the ones that are of interest to plugin and theme developers and contributors. I think that's best done with manual curation.

Something automated that describes the difference in the loading flow from one version to the next would be useful though.

@amitworx
Copy link

amitworx commented Dec 4, 2021

Thanks for sharing. Its really helpful.

@johnbillion
Copy link
Author

I've just been made aware of https://hookorder.com/ by Jānis Elsts which is a more comprehensive and better formatted way of viewing the load order and hooks that fire during a request to WordPress. Check it out!

@MQuigg
Copy link

MQuigg commented Jan 8, 2024

@johnbillion Very nice! We've been using his Admin Menu Editor Pro plugin for years. Thanks to both of you for this very useful information.

@richardbuff
Copy link

Awesome thanks for the link, it looks very useful!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment