You may have used a theme in the past, or are currently using one, that has a functions.php file in it. The Theme Functions file was introduced around the time that widgets came on the WordPress scene, usually containing just the code to register the dynamic/widgetised sidebars, however they’re useful for so much more. If you don’t already have a functions file then just create a blank file in your theme directory called functions.php.

Think of the functions file as a way to add PHP to your site without needing to code up a plugin everytime you want to make a change. The functions file can contain your own functions to make jobs quicker on the frontend, or they can use the filter hooks available to plugin authors, to manipulate the way a lot of the template tags work.

The great point with this file as well is that you can copy it across to each new theme to save you time on having to make your usual custom changes. I tend to spend a good hour or two adding in my own additional bits of code into a new theme eg. the social bookmarks I prefer to use, my recent posts list with the comment count after the posts etc., however with the functions file I can create a function to do all of this and then just reference it in my theme with one line.

Simple Function

A simple function would be to display the social bookmarks I use/display at the end of my posts. I have the following function in my functions.php file:

[sourcecode language=”php”]< ?php function display_sblinks($cssid = "social") { global $post; ?>

< ?php } ?>[/sourcecode]

The code above accepts a value for the id of the list, or if one isn’t set then it uses the default of ‘social’. Then it just outputs the list as normal, using the template tags for the URL and post title. Then all you would need is to place a call to the function when you want the list displaying ie.

<?php display_sblinks(); ?>

Or you can specify a different id name for the list eg.

<?php display_sblinks("sblist"); ?>

This is just a simple example of how you can use it.

Using a Filter

A more advanced function is to use one of the filter hooks to amend the default output. Last week I wrote about the wp_page_menu() function, and Dan Schulz made a comment with a good point, did we really need a div around the list, as a list is block level and could just have the specified class inserted into that.

So I had a play with a function and came up with the following code to go into your functions file (I don’t think it would warrant being a plugin to be honest). Feel free to use this of course 🙂

if (function_exists('wp_page_menu')) {
add_filter('wp_page_menu', 'pgmenu_filter');
function pgmenu_filter ($page_markup) {
preg_match('/^<div class="([a-z0-9-_]+)">/i', $page_markup, $matches);
$divclass = $matches[1];
$toreplace = array('<div class="'.$divclass.'">', '</div>');
$new_markup = str_replace($toreplace, '', $page_markup);
$new_markup = preg_replace('/^<ul>/i', '<ul class="'.$divclass.'">', $new_markup);
return $new_markup;

The first line of this code checks that the function wp_page_menu exists to avoid any errors if you’re not using WordPress 2.7. Then we have a filter, which takes the output of the function wp_page_menu and runs it through the function pgmenu_filter, which does the following

  • Line 4 and 5 gets the value of the div class
  • Line 6 and 7 removes the surrounding div from the output
  • Line 8 replaces the ul tag with a ul tag and a class attribute, with the value of the original div class
  • Line 9 then returns the new output

Bearing in mind the output would be something such as

[sourcecode language=”html”]


The new output would be

[sourcecode language=”html”]



So as you can see, the functions.php file is actually a fairly powerful file to have in your theme directory. You can remove a lot of that custom coding from your theme files into their own functions, and then all of your custom code is in one place and easy to change.