Under the Hood with WordPress, Part 5: PHP/MySQL

Tutorial

This is Part 5 of a 6-part series on diving “under the hood” with WordPress. Today, we’re covering PHP – the “PHP Hypertext Preprocessor” that runs, computes, analyzes, and stores your WordPress data.

Well, if you’ve made it this far, give yourself a silver star – you can now read and interpret the displayed code and styles that show up on the web!  However, the output you see on the front-end (the user-facing side) of the WordPress site isn’t the actual code being used – it’s computed code that has been rendered from information pulled from a database.

Keep in mind, this series is not meant to turn you into a PHP developer – it’s meant to allow you to recognize PHP and to interpret what’s going on.

What is PHP

PHP (PHP Hypertext Preprocessor) is a scripted programming language that runs when a user accesses a page or website.  Since it’s able to be embedded into HTML, PHP is both easy to learn (relative to other languages) and powerful enough to handle even the most complex actions on a site.

WordPress uses PHP to pull information, content, and options from a MySQL database and render that information out to the user.

Wait, MySQL?

MySQL (Structured Query Language) is, at its simplest form, a database that uses tables and rows to “relate” data together into queryable chunks.  A WordPress install uses upwards of 10 tables or more to keep options, content, and other variables separate from each other, but yet accessible and relatable should the need arise.

How it Works

When you visit a WordPress site, a number of things are happening all at once.  First off, content is being pulled from the database depending on which URL you’re visiting – specific page or post content, a category archive, or the home page.  Options for plugins are being recalled and – if certain flags are set in the database – code executes to satisfy those options.  The WordPress site checks for an active theme and, barring any errors, will pull that theme’s template files and display the data.  Finally, everything is compiled into one document and served up to the user.  All of this takes place in (ideally) a manner of microseconds.

PHP Structure

WordPress uses two major types of PHP syntax to do its thing.  Depending on where you look, you can see both of these types in action even in the default WordPress theme.

Functions

Found in both the functions.php file and the myriad of plugins you’ve installed, a function is a definition of a larger set of code.

<?php function writeMsg() {
  echo "Hello world!";
} ?>

This is about a simple of a function as you can get.  First, we use the <?php to tell the browser that we’re going to start running PHP code.  Then, we’re preloading the writeMsg() function to display the words “Hello World” any time that function is used.  Finally, we close the code with the ?> function.  The { and } signify that everything between them belongs to the function we just defined.

Template Tags

Template tags are technically also functions, but in the world of WordPress this is what we use in the templates and plugins to call the various defined functions we’ve written.  To execute the above function, we simply write:

<?php writeMsg(); ?>

Adding this to a WordPress template would show:

Hello World!

Hooks

There are a few special types of template tags that serve as “action points” inside of WordPress.  Called “hooks”, these areas allow plugins and themes to hook special functions in that are specific to certain areas of the page.  The two common ones are:

<?php wp_head(); ?>
<?php wp_footer(); ?>

The wp_head() hook is in the <head> section and allows themes and plugins to hook scripts and meta information.  SEO plugins and early-loading JavaScript files are usually called here to get them in before the rest of the page loads.

The wp_footer() hook is typically right above the closing </body> tag.  Plugins that need to load their scripts after the content – such as analytics plugins – hook here to make sure their scripts are loaded last.

While these may be the most “public facing” hooks, there are over 1350 hooks available to plugins and themes.

Filters and Actions

Hooks are divided into two distinct types – filter hooks and action hooks.

Filters

From the Codex:

Filters are functions that WordPress passes data through, at certain points in execution, just before taking some action with the data.

Think of a filter just as it sounds – a screen or check that data will pass through before it sends up in the database.  Using filters, we can actually add content to every “single” content piece (example from WPCandy):

<?php
	add_filter( 'the_content', 'wpcandy_filterhook_signoff' );
	function wpcandy_filterhook_signoff ( $content ) {
		if ( is_single() ) {
			$content .= '<div class="sign-off">Th-th-th-th-th That's all, folks!</div>' . "";
		} // End IF Statement
		return $content;
	}
?>
Actions

From the Codex:

Actions are triggered by specific events that take place in WordPress, such as publishing a post, changing themes, or displaying a page of the admin panel.

If you *do* something in WordPress, then it’s considered an action.  Actions allow a sequence of different functions to take place when a user or administrator performs that action.  For example, if you wanted to send an email every time you made a new blog post:

function email_friends( $post_ID ) {
   $friends = 'bob@example.org, susie@example.org';
   wp_mail( $friends, "sally's blog updated", 'I just put something on my blog: http://blog.example.com' );

   return $post_ID;
}
add_action( 'publish_post', 'email_friends' );