Skip to content

Working with WordPress Functions in Themes

The power of functions.php is often overlooked by WordPress theme designers. In my opinion, it is the single most important file in the theme.

The WordPress codex has this to say about the file:

A theme can optionally use a functions file, which resides in the theme subdirectory and is named functions.php. This file basically acts like a plugin, and if it is present in the theme you are using, it is automatically loaded during WordPress initialization (both for admin pages and external pages). Suggested uses for this file:

  • Define functions used in several template files of your theme
  • Set up an admin screen, giving users options for colors, styles, and other aspects of your theme

The "Default" WordPress theme contains a functions.php file that defines functions and an admin screen, so you might want to use it as a model. Since functions.php basically functions as a plugin, the Function_Reference list is the best place to go for more information on what you can do with this file.

Most themes these days do include a functions.php file. If, however, your theme does not then you can create the file yourself. Have a look at the functions.php file that is included in the Classic and Default themes. There is no harm in simply copying that file to your own theme directory.

Handy Hint:
For ease of management of custom functions I create a user-functions.php file and include this in functions.php like so:
include_once (TEMPLATEPATH . '/user-functions.php');

This makes it easy to separate out the theme functions from my own custom functions, which also makes it easier to reuse the custom functions across different themes.

Separating out the custom functions also means they are not overwritten when a theme is updated.

The functions.php file has been around for years. It was first introduced in WordPress 2.0. Yet, surprisingly, most theme designers don't seem to be aware of it. There is just so much that can be done with it (too much to cover here), however, if this post gets much interest or you ask nicely, I will write more about functions.php and provide examples of its use.

UPDATED with references to the codex. March 2008.

If you enjoyed this post, make sure you subscribe to my RSS feed!

Topic: WordPress
Tagged as: functions, PHP, Plug-in, WordPress Codex, WordPress themes

Share on FriendFeed

{ 1 comment… read it below or jump to the comment form to share your opinion }

  1. 1 Cecy June 19th, 2005 at 9:04 pm

    I am just starting to use wordpress and was looking for information like this. Thanks for sharing!

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>
Any comments that look like spam will be treated as spam - this includes SEO titles and use of spurious keywords.

By submitting a comment here you grant this site a perpetual license to reproduce your words and name/web site in attribution.