Thesis hooks. A complete newbies guide

November 3, 2009 · 15 comments

Want to make Thesis a gazillion times more awesome than it already is? Yes? Excellent! And remarkably simple.

What makes Thesis so clever is that it uses a bunch of ‘hooks’ to allow you to make changes to the look & feel of your Thesis website in seconds.

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 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 menu from above the header to below 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');

Note: Everything you’re seeing here goes on in the custom_functions.php file, contained within Thesis’ custom folder.

What’s happening 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. Here’s a visual example to help you understand.

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?

Subscribe for free WordPress tips & tricks

Every day I learn something new and interesting about WordPress. Subscribe to my spam-free newsletter and I'll send you a monthly digest with the best stuff I've found online.

Share this:

{ 13 comments… read them below or add one }

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.

Man November 1, 2010 at 2:25 pm

Just bought Thesis for a few days and still looking around for a useful hints on hooks and functions for a newbie cum non-technical guy like me.

Thankfully I’ve managed to discover your site, Dave. A simple yet meaningful guidance for me to start loving my thesis and make it worth every penny I’ve spent!

Sandy Boone February 14, 2011 at 11:01 pm

Thanks Dave! I know you wrote this a while ago, but you simplified it so that I understand the concept, after pulling out my hair for days trying to understand how the function idea worked! I could hug you right now. ; )

Dave February 22, 2011 at 3:28 pm

@Sandy, @Man you’re welcome. Glad it helped.

Michael February 26, 2011 at 9:55 am

Hi Dave,
Thanks for this tut! Really helpful for me the newbie

sharon April 9, 2011 at 6:35 am

hi, was wondering if you could help, I am getting familiar with hooks but once a hook position is placed how would I then remove it. Can I remove it using hooks again or do I need to go to custom_functions.php folder to do it. I want to remove a hook positioned above a footer, ideally simplest way possible. thanks. Harriet

Aliza Shehpatii April 16, 2011 at 8:07 am

I’m not a programmer, and I’m unfamiliar with coding thesis hooks, but you broke down creating Wordpress functions in a way that I can easily understand! Thank you!

Lisa November 19, 2011 at 4:29 am

I just started using Thesis. I really appreciate this tutorial, I have so many things to learn still!

Leave a Comment

{ 2 trackbacks }

Previous post:

Next post: