With over 15 years of experience working with WordPress, I‘ve seen firsthand what really happens behind the scenes when a user requests a page.
In this comprehensive guide, I‘ll walk you through the step-by-step process of what happens in WordPress from the time a user requests a page to when it‘s fully loaded on their screen.
I‘ll also share insights, statistics, tips, and code snippets you can use to optimize your own WordPress site. Let‘s get started!
Contents
- WordPress Usage and Popularity
- Step 1: Browser Sends Request
- Step 2: Web Server Receives Request
- Step 3: WordPress Bootstrap File Loads
- Step 4: WordPress Configuration File Loads
- Step 5: Core WordPress Files Load
- Step 6: Active Theme Loads
- Step 7: Active Plugins Load
- Step 8: WordPress Parses the Request
- Step 9: WordPress Performs Database Queries
- Step 10: WordPress Generates the Response
- Step 11: Web Server Sends Response
- Tips to Optimize Your WordPress Site
WordPress Usage and Popularity
To understand why it‘s important to know how WordPress works, let‘s look at some stats:
-
Over 455 million websites run on WordPress – that‘s over 40% of all websites!
-
WordPress handles over 20 billion pageviews per month.
-
Over 15,700 plugins and 8,000 themes are available to extend WordPress functionality.
It‘s clear that learning WordPress can benefit web designers, developers, bloggers and site owners alike.
Step 1: Browser Sends Request
Everything starts when a user types your WordPress site‘s URL into their browser‘s address bar and hits enter.
The browser then sends an HTTP request to your web server asking for that specific page.
GET /blog/how-wordpress-works HTTP/2
Host: yoursite.com
Step 2: Web Server Receives Request
Your web server receives the GET request from the browser.
It passes the request to PHP which acts as the processor for WordPress.
PHP will check the .htaccess
file to determine which WordPress files need to handle this request.
.htaccess file:
# WordPress rewrite rules
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Step 3: WordPress Bootstrap File Loads
The index.php
file in the root WordPress directory acts as the bootstrap file. It initializes the WordPress core software and tells WordPress to load.
index.php:
<?php
/**
* Front to the WordPress application. This file doesn‘t do anything, but loads
* wp-blog-header.php which does and tells WordPress to load.
*/
/** Loads the WordPress Environment and Template */
require( dirname( __FILE__ ) . ‘/wp-blog-header.php‘ );
Step 4: WordPress Configuration File Loads
Next, WordPress loads the wp-config.php
file which contains database credentials and site configuration details.
This allows WordPress to connect to the database and load all the core files it needs.
wp-config.php:
// ** MySQL settings - You can get this info from your web host **
/** The name of the database for WordPress */
define(‘DB_NAME‘, ‘database_name_here‘);
/** MySQL database username */
define(‘DB_USER‘, ‘username_here‘);
/** MySQL database password */
define(‘DB_PASSWORD‘, ‘password_here‘);
/** MySQL hostname */
define(‘DB_HOST‘, ‘localhost‘);
Step 5: Core WordPress Files Load
Now that WordPress can connect to the database, it will load all the main WordPress system files.
These core files are located in the wp-admin
and wp-includes
directories.
This gives WordPress access to all the required classes, functions, hooks and APIs needed to operate.
Some important files loaded at this stage:
- wp-settings.php: Core settings and configuration
- wp-includes/functions.php: Common WordPress functions
- wp-includes/class-wp.php: Core WordPress class
- wp-includes/class-wp-query.php: WordPress database query class
- wp-includes/plugin.php: Handles WordPress plugins
Step 6: Active Theme Loads
Based on the active theme set in the WordPress database options table, WordPress will now load the main theme files.
These usually include:
- functions.php: Theme functions
- header.php: Site header template
- footer.php: Site footer template
- index.php: Main theme template
Child themes will load first, followed by the parent theme files.
Step 7: Active Plugins Load
WordPress then loads all the active plugins on your site.
The list of active plugins is stored in the active_plugins
option in the options table.
This allows WordPress to only load the plugins that are enabled.
Plugins add all kinds of extra functionality to WordPress through hooks and filters.
Step 8: WordPress Parses the Request
Now that everything is loaded, WordPress will parse the original request to determine what content the user wants to see.
It matches the request against the permalink structure rules to see if it matches a specific page, post, or archive.
Some examples:
yoursite.com/about
-> Pageyoursite.com/blog
-> Posts pageyoursite.com/category/wordpress
-> Category archive
Step 9: WordPress Performs Database Queries
Based on the request, WordPress will query the MySQL database to get the required content like posts, pages, categories etc.
The results are stored in the $wp_query
object.
// Sample WP_Query to get latest 3 posts
$args = array(
‘post_type‘ => ‘post‘,
‘posts_per_page‘ => 3
);
$wp_query = new WP_Query( $args );
Step 10: WordPress Generates the Response
Now WordPress has the template files, active theme functions & plugins, and the database query results.
It will combine everything to generate the full HTML output for the requested page.
The HTML response might look something like this:
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<header>
<!-- Site header from header.php -->
</header>
<div class="content">
<!-- Main post loop generated by index.php -->
<p>Post excerpt...</p>
</div>
<footer>
<!-- Site footer from footer.php -->
</footer>
</body>
</html>
Step 11: Web Server Sends Response
The web server takes the fully generated HTML page from WordPress and sends it back to the user‘s browser.
The browser then renders the HTML and displays the page on the user‘s screen.
This whole process happens in just a few milliseconds!
And that‘s how WordPress handles billions of page requests and runs over a hundred million sites around the world. Pretty cool!
Tips to Optimize Your WordPress Site
Now that you understand how WordPress works, here are some tips to optimize your site:
-
Use a caching plugin – Caching reduces database queries and improves load times. Try a plugin like WP Rocket.
-
Minify CSS/JS – Minification removes whitespace and shortens file names to optimize performance. Plugins like Autoptimize can help.
-
Optimize images – Compress large images and enable lazy loading to improve load times. Use EWWW or Imagify.
-
Limit plugins – Too many plugins can slow down your site. Only use essential plugins and regularly review them.
-
Tune your database – Optimizing database tables and queries can significantly improve site speed.
-
Use a content delivery network (CDN) – A CDN stores static files in cache on servers around the world for faster delivery.
Let me know in the comments if you have any other tips!
I hope this guide gave you a better understanding of what happens behind the scenes when you use WordPress. Leverage this knowledge to create faster, more optimized sites.