Thesis hooks. A complete newbies guide

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?

  • http://www.kimwoodbridge.com Kim Woodbridge

    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

      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…

  • Pingback: (Anti) Social Lists 11/15/09 | (Anti) Social Development

  • http://ilovethesis.com Ramses

    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

      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.

    • http://aldosoft.com/ Alderete

      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

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

  • Pingback: How to Include a Post on a WordPress Static Front Page with the Thesis Theme | WordPress and Facebook Consultant | (Anti) Social Development

  • http://www.malaysiandiet.com Man

    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!

  • http://sandyboone.com Sandy Boone

    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

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

  • http://michaelmiello.com Michael

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

  • http://www.animalcharities.net sharon

    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

  • http://www.thenewsnation.com/ Aliza Shehpatii

    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!

  • http://appliedbehavioranalysis.com Lisa

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

  • http://www.bloggingtalks.com Samrat kafle @ Blogging Talks

    Thanks for the tutorial really helpful tutorial for thesis

  • rajesh

    how to remove [..] symbols from the teaser, continuation symbol in thesis. please tell me the solution to remove this symbol from the thesis teaser.