Thesis hooks. A complete newbies guide

by Dave on November 3, 2009

in Thesis Recipes

So you bought Thesis because everyone said it was a breeze to customize eh? WRONG! Unless you know stuff about (a) HTML, (b) CSS, (c) PHP, (d) WordPress and (e) Thesis, it’s not easy.

So either you need to learn some stuff or you need to find a new WordPress theme framework. I’d suggest learning. It’s far more rewarding.

With that in mind, let me show you how you can use ‘Thesis hooks’ to make changes to the look & feel of your Thesis blog in seconds. Everything you’re seeing here goes on in the custom_functions.php file, contained within Thesis’ custom folder (you could also use the excellent Openhook plugin, but that would be too easy).

Thesis actions & hooks

In WordPress, and in Thesis, hooks are used to make theme development easier. Think of a hook as a placeholder in your theme, e.g. thesis_hook_before_header is Thesis’ hook/placeholder for stuff you’d want to place in your theme, before your header (the bit that contains your logo and tagline).

A great example of hooks in use is an action used to switch the Thesis navigation bar from the top of the header to the bottom of the header, like this:

// Place Thesis Navigation beneath the Logo

remove_action('thesis_hook_before_header' , 'thesis_nav_menu');
add_action('thesis_hook_after_header' , 'thesis_nav_menu');

What’s happening here is that we’re removing the thesis menu (thesis_nav_menu) from its default hook position of thesis_hook_before_header and adding a new action (containing the same thesis_nav_menu function) to the thesis_hook_after_header hook position.

There are a lot of hooks available to you in the Thesis theme, which is one reason why the theme framework is so versatile. DIYthemes have prepared a complete list of Thesis hooks and Greg Rickaby has created an excellent visual overview of the hooks positions.

Thesis functions

A function is a bit of code that makes something happen to your Thesis-driven blog. Functions, not hooks, are the bit that confuse Thesis beginners. The odd thing about functions is that they’re ridiculously simple once you understand what’s going on. Let’s take a look:

function custom_function() {
?>
	<p>Insert your HTML code here...</p>
<?php
}

What’s happening here is that:

  1. You’re telling Thesis that you would like a new function, called custom_function in this case (it can be called anything you like, although your function name should relate to what it does, e.g. footer_credits(), header_links(), etc.).
  2. You’re adding an opening curly bracket { to start the function.
  3. You’re closing out your PHP code by using ?> (this allows you to add HTML in the next section).
  4. You’re adding some HTML using the <p> tag.
  5. You’re re-opening the PHP code so the function can complete itself using <?php.
  6. You’re ending the function using a closing curly bracket }.

You can add anything you like to the HTML section. You could add a div, some PHP, some JavaScript, an image, a whole new page. Anything.

Hooks and functions together

Now let’s say you wanted to add some links to the header of your site. Perhaps you’ll link out to Facebook, Twitter and your RSS feed. We already know (from the DIYthemes hooks list) that the hook (or placeholder) we want to use to place our new links is thesis_hook_header . So all we need to do is (a) create a new function and (b) add the hook. Something like this:

// Add custom links in the header

function header_links() {
?>
	<p id="header_links"><a href="#">Facebook</a> | <a href="#">Twitter</a> | <a href="#">RSS</a></p>
<?php
}

add_action('thesis_hook_header','header_links');

Take a closer look:

  1. We’re defining a new function (header_links).
  2. We’re using the curly bracket to open the function.
  3. We’re closing out our PHP to allow for some HTML.
  4. We’re adding our HTML.
  5. We’re re-opening PHP.
  6. We’re closing out the function with a curly bracket.
  7. And finally, we’re adding the function header_links to a hook thesis_hook_header.

Ohhhhhh. Simple, isn’t it?

Liked it? Share it
  • Twitter
  • StumbleUpon
  • del.icio.us
  • Digg
  • Facebook
  • LinkedIn
  • Posterous
  • Tumblr
  • email

About Dave

Hello. I'm Dave, and I built the Thesis skin you're looking at right now. Why? Because along with PSD to Thesis services, that's what I do. I'm also a lousy poker player. Follow me on Twitter if you're feeling brave, or bored.

{ 6 comments }

Kim Woodbridge November 3, 2009 at 6:13 pm

Great tutorial Dave! Hooks seem so confusing but once you start using them you realize it’s not all that mysterious.
.-= Kim Woodbridge´s last blog ..12 Free WordPress Moon Themes =-.

Dave November 4, 2009 at 6:19 am

Thanks Kim. That’s exactly how I feel. When I first started using Thesis hooks scared me off. Then the penny dropped. Now I think they’re awesome and I want to have their babies. OK, maybe the Wilkinsons’ have enough babies for the time being, but you know what I mean…

Ramses November 18, 2009 at 1:40 pm

Came here from twitter.com – Indeed a very clean customization you got here :-)

But why don’t we all just use the OpenHook Plugin? No custom functions hack-hack needed anymore.
.-= Ramses´s last blog ..Thesis Theme Marriage – Divorce Finally Legal =-.

Dave November 18, 2009 at 1:55 pm

That’s a really good point. Openhook is great as a free plugin. It’s worth every penny. But after it managed to lose my data twice, I’m very wary.

I also dislike the fact that Openhook doesn’t store (as far as I know) your custom functions in Thesis’ custom_functions.php file. So if you have to move your theme from one server to another (which I do A LOT) you’re constantly having to copy/paste code. I also tend to disagree with your ‘hack-hack’ statement. In my opinion, adding code to the custom_functions.php file is the clean, proper way to work. Using Openhook is the hack.

In summary I think it’s an awesome plugin, but I wouldn’t trust it on commercial projects.

Alderete November 20, 2009 at 11:34 am

I’m with Dave, the OpenHook plugin was nice when you couldn’t edit custom_functions.php via the web interface, but now that you can, it’s more of a crutch than a tool. If you’re going to customize Thesis via hooks, you really ought to learn the built-in, standard, fully-supported way to do so: custom_functions.php.

Putting your customizations into custom_functions.php, rather than OpenHook, has two big advantages:

– Your customizations are stored in the theme, in the thesis/custom folder. You can take them with you to a new blog just by copying the directory. They are easy to back up. They are easy to put under version control. If you use OpenHook, you have to [remember to] copy/paste from a bunch of web form fields into other web form fields on a new site. Not as simple. It’s harder to back up your customizations. And forget about version control.

- OpenHook stores your hook insertions in the database. Which means it adds extra database queries, for every page view. Which means a performance hit. ‘Nuf said.
.-= Alderete´s last blog ..Applying Thesis styles in print =-.

Dave November 20, 2009 at 11:44 am

I love it when people think I’m right! Doesn’t happen often, but when it does, it rules! Thanks for the comments.

Comments on this entry are closed.

{ 1 trackback }

Previous post:

Next post: