// action hook creating the footer thematic_footer();

Save all your changes, jump into your browser, and head on over to your child theme's home page. If all went well, your list of pages will be waiting for you at the bottom. Now all you need to do is add some CSS to make them pretty, and you're ready to rumble! Here are some starter styles that display the items horizontally and lose those pesky bullet points:

chapter_05/v2/wicked/newstyles.css (excerpt)

body.home ul#simplepages { width: 940px; margin: 0 auto;

body.home ul#simplepages li { list-style-type: none; display: inline; margin-right: 30px;

What we've learned here is how to insert some new content using WordPress's template tags, and how to take advantage of WordPress's custom template includes. That's great, but there's one more big piece of the WordPress and Thematic puzzle that you'll need to master: hooks and filters.

Hooks and Filters

In the above example we put the markup generation inside a function in our functions.php file, but we still had to edit a template file to insert a call to that function. This works, but it turns out we can do this in an even cleaner way: WordPress exposes two different kinds of hooks, so called because code attached to a hook grabs on to WordPress's functionality like Velcro. We can create new functions in functions.php, attach them to these hooks, and have them modify parts of the theme's output without touching any template files.

Action hooks are provided for events, such as when a post is displayed or a page's footer is rendered. You'll commonly use an action hook when you want to attach a function you've written to a certain activity—for example, when WordPress displays a comment or widget.

Filter hooks allow you to pass content through a function and return it to WordPress—generally, these are used to modify text. For example, if you wanted to highlight certain words in your post's content, or append a special icon image to links, you'd use a filter hook on the content of your posts.

In both cases, you write your function, then write a simple line of code that registers your hook with WordPress. Then, when WordPress wants to display content or take some action that triggers one of those hooks, it goes looking for any functions that were registered to it, and executes them. No need to worry if this seems confusing right now: as soon as you've seen a few practical examples it will make perfect sense.

This book is far too small to explain every single hook in WordPress—there are over 1,100 of them! You'll find a list of all the actions available to you in the WordPress Codex Action Reference.6 For filters, check out the WordPress Codex Filter Reference.7 Another great reference is Adam R. Brown's WordPress Hooks Database,8 which includes information on which hooks and filters were added or removed in each version of WordPress; you'll find that this information is absolutely essential if you're developing themes to be backwards-compatible with older versions of WordPress.

Yup, WordPress sure does provide a whole lot of hooks. It's okay—many of these hooks are about stuff like posting or pingbacks, and these are better suited to plugins. The most useful hooks for themers are the ones that relate to displaying markup and content.

Hooks are simple enough to use—write your function and then tell WordPress you intend to hook into it. Adding an action or filter hook is done with functions named add_action and add_filter, strangely enough:

^JS Wait a minute ... eleven hundred hooks?!

6 http://codex.wordpress.org/Plugin_API/Action_Reference

7 http://codex.wordpress.org/Plugin_API/Filter_Reference

8 http://adambrown.info/p/wp_hooks/

add_action($tag, $function_to_add, $priority, $accepted_args); add_filter($tag, $function_to_add, $priority, $accepted_args);

The $tag parameter is where you specify the hook you want to add to; $function_to_add is the name of your function. The next two are optional, but worth knowing about: $priority is used to determine the order in which your action ought to occur, relative to other actions that might be happening on the same hook—larger numbers happen later. $accepted_args is a number that tells WordPress how many arguments your function will expect to receive.

If you decide that you want to "unhook" an action or filter from a hook, that is simple too. Call WordPress's remove_filter. It's constructed in much the same way, with one slight caveat: you can't call it directly, so you need to hook it onto WordPress's init action. init is an action hook that WordPress runs as soon as it loads. By delaying our remove_some_filter function slightly, we ensure that the action we're trying to remove has already been added; otherwise this would produce an error. Here's how you use remove_filter:

function remove_some_filter() {

remove_filter(tag, function, priority, args);

add_action('init', 'remove_some_filter')

What happened to remove_action, you wonder? It turns outthatit's an alias for remove_filter: when you call remove_action, WordPress simply calls remove_filter, which can remove either action or filter hooks. If you'd rather keep your code readable by always using remove_action for hooks you know to be actions, go right ahead. If you find it simpler just to use remove_filter all the time, then feel free to do that instead. Either approach is quite correct.

If an action or filter was assigned a priority when it was added, you must specify the same priority if you remove it. Otherwise WordPress won't remove it, nor give you any clues as to why not. This can be particularly frustrating if you're unaware of this rule! So remember: if remove_filter is failing, be sure to check if the hook was added with a priority.

A thoughtfully developed theme framework will also bring its own hooks to the table, each of which you can augment, remove, or replace. Thematic, of course, provides plenty—we'll talk about those in more detail later.

What about remove action?

Get Your Priorities Straight

Was this article helpful?

0 0

Post a comment