WordPress began it’s life as a blogging platform, but has since morphed into fully-fledged Content Management System (CMS from here on out). What exactly is a CMS, you ask?
A content management system is an application (WordPress core software) or set of programs (think plugins) that work together to allow a user to create and manage digital content.
That’s nice and all, but how exactly does WordPress take that text about “10 Potato Chips That Look Like Abraham Lincoln,” and display it to a user across the world when someone goes to the URL buzzfeed.com/10-chips-lincoln? How does WordPress maintain that content and display it. There’s a few layers to this story.
The WordPress Core
The WordPress Core is where much of the base content managing happens. The Community has already taken it upon themselves to create the structure for creating and saving content. At its base, WordPress is a PHP application that saves data to a MySQL database. Which is why those are the only two real requirements of WordPress.
When you first download WordPress and perform the famous 5-minute install, the software creates a SQL database that contains 11 database tables. These tables store data for site settings like the default URL (wp_options), user information (wp_users & wp_usermeta), post content, images, and post metadata (wp_posts & wp_postmeta), category and tag information for posts (wp_terms, wp_term_relationships, wp_term_taxonomy), comments on posts (wp_comments & wp_commentmeta), and of course the deprecated links feature (wp_links). Although it can be turned back on.
The table that is likely to get the most use in your WordPress install is the wp_posts table. This is the table where all of your content is stored. Anytime you create a post in the WordPress backend, the software is creating a new entry in the wp_posts table with a unique numerical identifier. The post ID.
This is where things can get a bit confusing due to the terminology WordPress carries over from starting as a blogging platform. Any bit of content created is typically referred to as a post. When you hit Add New under the Posts tab in the WordPress dashboard you are creating a post of type Post. When you hit Add New under Pages, you are creating a post of type Page. Confused yet? Just wait until we throw custom post types into the mix. The main takeaway is that Post, Page, and Custom Post Type data is stored in the wp_posts table.
For more information on what exactly is stored in each table of the WordPress database check out the Codex.
How a Post Becomes a Webpage
At this point you’ve used the WordPress software to add data into a SQL table somewhere on a server. Your content is thoroughly managed, and ready to spring forth into the the world. It may seem difficult, but don’t worry the WordPress core comes fully stocked with functions for delivering your content to the web.
You Lost Me At Function
Functions are blocks of code that perform a specific task. WordPress has a function for nearly anything you could imagine. Want to get the posts from the database? You could use the get_posts function.1 If you’re editing your theme and you want to put a post or page title in a certain spot of your HTML you could use the the_title function. That function gets the title of the current post and outputs it onto the page. That current post is determined by where we are in The Loop.
The Loop, There It Is
Ah the Loop. The ever beating heart of WordPress. This is a nifty little PHP function created for one thing, and one thing only. Getting your content from the database and allowing it to be displayed onto a PHP based webpage. There’s a bit more to it than that, so let’s break it down.
“The Loop is PHP code used by WordPress to display posts. Using The Loop, WordPress processes each post to be displayed on the current page, and formats it according to how it matches specified criteria within The Loop tags. Any HTML or PHP code in the Loop will be processed on each post.” – WordPress.org Codex
The Loop is an if / while loop. Meaning it first checks if we have any post data in our database. Then while we have post data it performs any HTML or PHP we have within the the while for each individual post. That allows us to use predefined functions from WordPress for displaying the title or post content, or even create our own functions to run.
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php // post content goes here ?>
<?php endwhile; else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
Break It Down
The first part of the Loop is an if statement. It is using the have_posts() function to determine if your current WordPress query has found any posts in your database. If there are posts, have_posts() will return true. Making our if statement pass and move into our while statement.
The while statement also checks if we have_posts(). Any code within the while statement will run once for each post found. Essentially if we have 5 posts returned, have_posts() will return true for all 5 posts, then switch to false and end our while loop. If we didn’t have that condition our while loop would never end and we’d crash the browser.
The last function you’ll see in The Loop is the_post. This is function gets the next post and sets up its data. This little function allows WordPress to know we’re in The Loop and properly pull in data from template tags like the_title and the_content.
Make Me Pretty
Now that we know how WordPress handles data we need to get it displayed on our site so a user can see it.
The theme handles displaying WordPress content. It is the coat of paint on the WordPress data. When you first install WordPress you’ll be active with the newest twenty[year] theme that ships with the software. If you didn’t have a theme, you would have nothing to display to a user.
Most of the editing you’ll do to alter WordPress for your site will be in the theme, but there is another area of expandability, plugins.
Plugins extend the WordPress core functionality. Want to sell something on your site? You would use a plugin like WooCommerce or Easy Digital Downloads to get that done.
There are nearly 50,000 plugins available2 on the WordPress.org repository that do everything under the sun from securing your site to allowing you to sell products. Plugins can range from full blown applications to a couple lines of code for adding a message to the admin dashboard.
That’s the basics of how the database, WordPress core, themes, and plugins work together to allow you to create and display content to anyone with an internet connection. If you want to dig even deeper into the inner workings of WordPress check out Up and Running by WPShout. They have a fantastic metaphor explaining WordPress as a factory.