If you are using a commercial theme for your WordPress site, chances are you have customized your site using numerous action hooks or filter hooks. Theme hooks make it easy to customize, while still allowing the theme developer to enhance their code. Theme hooks also make a mess of your code and can make it hard to trace which code is affecting which area of your site.

I have recently done three different site redesigns that involved jettisoning a commercial theme in favor of developing our own. These sites ended up using no theme hooks or filters of their own, resulting in code that is far easier to customize and understand.

Why theme hooks and filters are great

The sign of any great WordPress theme or framework is a robust set of well-documented theme hooks and filters. One of the best frameworks out there is Genesis, which has something on the order of 50 action hooks, plus at least another 50 filter hooks. You drop in a function and attach it to your hook via the functions.php file. For example, this is how you would insert a div with the characters “foo” right before the #footer div.

function my_function() { 
  echo '<div>foo</div>';
}
add_action ('genesis_before_footer', 'my_function');

In a similar way, a filter hook could alter your bylines like this.

function post_info_filter($post_info) { 
  $post_info = 'by Famous Author';
  return $post_info; 
}
add_filter( 'genesis_post_info', 'post_info_filter', 20 );

Filters and hooks give you programmatic spots throughout a theme where you can manipulate text or cause other actions to occur. By encapsulating these customizations in a hook or filter, the theme developer can continue to enhance the theme without having to worry about how to apply updates to your custom code.

Why a theme without its own hooks and filters is great

I spent years using really useful themes like Genesis, Canvas and Thesis. I spent those same years surfing countless online forums for how to customize all of the theme hooks and filters to suit my needs. The result? The result was a functions.php file that contained over 2000-3000 lines of code, calling all manner of hooks within the theme. It ended up being difficult to read and sometimes difficult to track down where various customizations were taking place.

When you develop your own theme, you are able to put the logic exactly where it is occurring in the flow of outputting your page. Using the example above that alters your bylines, you would skip functions.php entirely. Simply find the spot in your template where you want the byline to go and place your text there. If you want to apply conditional logic to it, just put that logic right where it belongs.

<h1>Page Title</h1>
<?php if (is_single()) { echo 'by Famous Author'; }
<p>Article text goes here</p>

When you start adding hundreds of such customizations, it becomes much, much easier to read and understand your code. It is also very intuitive when trying to find the location on the page that you are trying to alter.

Unlike a commercial theme, our theme has exactly zero of its own hooks or filters in it. The only hooks we use in our page templates are wp_head() and wp_footer(), both of which are critical to WordPress and plugin functionality.

I’m not talking about WordPress or plugin hooks and filters

Keep in mind that when you create your own theme from scratch, you are still going to use lots of built-in WordPress hooks and filters. Maybe you need to register a custom post type in the init hook, alter a query using pre_get_posts or customize post excerpts using the excerpt_more filter. That is how you interact with WordPress, and using those hooks is essential.

The same thing applies to plugins. A plugin can expose an action hook or a filter to allow you to customize what the plugin is doing. Unless you want to start maintaining the plugin code yourself, you will only use hooks and filters made available by the plugin.

Developing your own theme isn’t for most folks

Most people running their own website are not going to venture in to writing their own theme and will use a variety of commercially-available themes. In that case, using hooks and filters made available by your theme developer is certainly the best option.

For the small percentage of folks who have the time and skills to create your own theme, however, you will be rewarded by a much more flexible and easy to understand theme.

Latest Developer Blog Articles

Job Listings on GeekWork

Find more jobs on GeekWork. Employers, post a job here.