Blog

What is: Template Tag

A template tag is a PHP function used to generate and display information dynamically. WordPress Themes contain different templates and theme developers use template tags to fetch and display dynamic data. WordPress has many built-in template tags that can be used in WordPress themes. WordPress plugins and themes can also define their own template tags and use them in different templates.

Example:

<?php the_author(); ?>

The author template tag displays the name of the post author in WordPress.

Usage example:

<p>This post is written by <?php the_author(); ?></p>

Template tags can also return a data set and users can choose what to display using parameters.

Example:

<a href="<?php bloginfo('url'); ?>" title="<?php bloginfo('name'); ?>"><?php bloginfo('name'); ?></a>

Template tags are basically PHP functions, so any PHP function defined by a WordPress plugin or theme can be used as a template tag. To use a theme function as a template tag, the function should be defined in the theme’s functions.php file.

Template tags are PHP functions, so they can also be used inside other PHP functions and template tags. In the example below, we have defined a function that displays some text.

Example:

function donation_request() {
    $this_article = wp_title('',true);
    echo '<p>Hi, if you enjoyed reading '.$this_article.' please consider <a href="http://www.example.com/donate/">donating</a>.';
}

To use this function in a template, add this line of code:

<?php donation_request(); ?>

Multiple template tags can also be combined to accomplish a goal.

This post was originally published in the wpbeginner glossary.

Additional Reading