The Anatomy of a Template Tag in WordPress

By Lisa Sabin-Wilson

Template tags can be useful when making theme decision on your blog. Before starting to play around with template tags in your WordPress templates, it’s important to understand what makes up a template tag, and why.

WordPress is based in PHP (a scripting language for creating web pages) and uses PHP commands to pull information from the MySQL database. Every tag begins with the function to start PHP and ends with the function to stop PHP. In the middle of those two commands lives the request to the database that tells WordPress to grab the data and display it.

A typical template tag looks like this:

<?php get_info(); ?>

This entire example tells WordPress to do three things:

  • Start PHP (<?php).

  • Use PHP to get information from the MySQL database and deliver it to your blog (get_info();).

  • Stop PHP (?>).

In this case, get_info is the actual tag function, which grabs information from the database to deliver it to your blog. What information is retrieved depends on what tag function appears between the two PHP commands. As you may notice, a lot of starting and stopping of PHP happens throughout the WordPress templates. The process seems as though it would be resource intensive, if not exhaustive — but it really isn’t.

For every PHP command you start, you need a stop command. Every time a command begins with <?php, somewhere later in the code is the closing ?> command. PHP commands that aren’t structured properly cause really ugly errors on your site, and they’ve been known to send programmers, developers, and hosting providers into loud screaming fits.