As a WordPress hosting client, you don’t need to know how WordPress works behind the scenes. In fact, WordPress was created so that people could focus on writing and publishing, not on the technical details. But it’s useful to have a basic grasp of what’s happening under the hood of your WordPress site. It may help you to diagnose problems more easily and understand why WordPress behaves as it does.
The Building Blocks of a WordPress Site
Carl Sagan once said that “if you want to make an apple pie from scratch, you must first create the universe.” Something similar is true of a complex piece of software like WordPress. If you wanted to fully understand WordPress, you’d have to be a developer, a network engineer, a database administrator, and a designer. It would take (several) books to explain the WordPress universe from scratch, so let’s stick with the basic building blocks.
WordPress is a PHP application that relies on three key components: a web server, a database, and a programming language interpreter.
- Apache. Apache is a web server. It is responsible for listening for requests from a browser and sending a response – the web page – back. After Apache receives a request, it gives the URL and other information to WordPress, which then generates the web page.
- MySQL. MySQL is a relational database – it stores information in tables. All of the blog posts on a WordPress site are stored in a table called wp_posts, for example.
- PHP interpreter. WordPress core is a collection of files that contain code written in the PHP programming language. It is the execution of these files that generates web pages.
The PHP files can be divided into two basic groups: template files and files that contain functions – reusable chunks of code – and constants that are used in templates and elsewhere in WordPress’ code. WordPress is a content management system, so the template files are the most interesting from our perspective. They generate the web pages.
Generating a HTML Page
When the Apache web server receives a request for a page, WordPress first runs a number of set-up scripts, including the wp-config.php file that provides information such as which database WordPress should use.
After the set-up phase is complete, WordPress starts the process of generating an HTML page by executing a template file. The template files are a mixture of HTML and PHP. The role of the PHP code is to fetch data from the MySQL database and to process it into HTML. Exactly which HTML is output depends on various factors: the available data, which user is logged in, the time of day, and more. Other functions are executed on template pages, but the ultimate goal is to dynamically generate an HTML page that can be sent to the requesting browser.
There are many template files and which one is executed depends on a complex set of rules, but is primarily determined by the URL of the request. For example, if the URL is the site’s home page and the site is configured to display a list of blog posts, the home.php file is executed, unless there is a front-page.php file, in which case that is executed.
The full template hierarchy for the front page looks like this:
There are templates for posts, pages, tag indexes, category indexes, the 404 page, and more. Most of them are provided by the site’s theme.
You might have noticed that we haven’t mentioned plugins yet. Plugins can change the HTML that is rendered on a page, but their code does not live in the template files. Instead, WordPress provides a number of hooks onto which people can hang their own code. Hooks are the foundation of an event-based system for extending WordPress. Developers can register code to be executed when an event occurs. There are dozens of events developers can hook into, such as wp_loaded, which runs after WordPress is fully loaded, and pre_get_posts, which runs before a database query.
Hooks come in two varieties: action hooks and filter hooks. The difference between them is not important for our explanation of how WordPress works. It is enough to understand that the code associated with hooks can carry out actions and change the HTML that is output. For example, a hook can be used to add a widget to a page.
WordPress plugins use hooks to integrate their functionality with the HTML rendering process, and they’re one of WordPress’s major strengths. WordPress would be much less powerful if it didn’t provide a mechanism for developers to extend its functionality.
As we said at the beginning of this article, there is a lot more happening beneath the hood of a WordPress site than can be explained in a blog article, but we hope that reading this has given you a useful understanding of what happens when someone loads a page on your WordPress site.