wordpress: modifying themes

Common Theme Functions

Since template files are PHP documents, each template is capable of requesting information from the server. For example, the header.php template will generally request the blog title and tagline from the database and insert those values at the right place. Meanwhile, the sidebar template will look to see which Widgets need to be displayed, and the single.php template will grab the article body, byline, timestamp, and any comments from the database.

All of these calls for information from the database are handled by custom PHP "functions" specific to WordPress. For example, take a look in the header.php template and you might find a line something like this:

In other words, the destination of the link is being replaced by the results of a call to a PHP function called home_url(). That function isn't built into PHP itself - it's a function provided by WordPress. Its value is whatever is set as the homepage URL in the Dashboard's General Setting section. The cool thing about setting a link back to the homepage this way is that it makes the theme portable. If one WordPress installation is in the root domain of example.com but another site has a blog at example.org/blog, both sites can use the same theme, and the homepage links will resolve perfectly. By avoiding hard-coding as much as possible, the theme stays adaptable to many situations and applications.

But how did the theme author learn of the home_url() function? By consulting the dictionary of WordPress Template Tags and the WordPress Function Reference, where dozens of such tags are documented. Let's take a look at a few common ones:

get_bloginfo(): You'll see this one referenced in header templates a lot. By giving it "arguments," you can obtain most of the settings from the Dashboard's General Setttings page. For example, try putting this in one of your templates:

The title of the site will be inserted in the template wherever you drop this tag. So what if you want to refer to an image that lives in an "images" subdirectory of the theme directory? You can do it like this:

Now the image will still work even if you later decide to rename your theme folder!

In addition to the dozens of template tags that come with WordPress, many plugins will provide you with additional ones as well. Themes can also define their own custom functions in functions.php. If you see a function called from your theme that you can't find listed in the Codex, then you'll know it's either provided by a required plugin or from that theme's own custom functions.

Function or Template Tag?

There's one important thing to be aware of when working with these: Most functions have two versions - one for displaying the value immediately into the web page, and another that stores values in memory for further processing. You can distinguish between them by the presence of "get_" in the name. For example, bloginfo('name') will display the site's name directly, but get_bloginfo('name') will not. The correct usage of the latter might be something like:

This would cause the value of the site's name to be stored in the variable $sitename, then run through PHP's "lowercase" function. The result would be written to the page by the PHP echo() function. For most non-programming purposes, stick to the Template Tags, not the Function Reference.

There are far too many template tags and functions to cover here, but you get the idea. If you come across an unknown function while working on a theme, just look up its name in the Codex to learn what it does and what kinds of options or arguments it takes. Template Tags are wonderfully powerful!

Remember: Avoid hard-coding values into your theme whenever possible.