How to use hooks in Genesis

Posted on Posted in Blog

Hooks in Genesis allow you to ‘hook’ into your theme at specific places and add content. How do you know what the different hooks are? Refer to the Genesis hooks library.

An example hook

Here’s an example of how to use a hook in Genesis. We’re going to be using the before_header hook, but remember that they all work the same way.

[gist id=5236640]

If you want to try this out – hopefully you do – whack it at the bottom of your functions.php. Don’t forget to take a backup of the file first in case you break anything. I’ve tested the code and it works, but you never know what might happen!

Let’s talk a little bit about what’s going on here. On the first line we tell WordPress we’re about to write a function called function_name (in real life, try to use descriptive names for your functions so that when you come back to them you can remember what they’re about).

On line 2, we use the most elementary PHP command there is, echo. In English, the line would read, ‘Print the string of characters enclosed in quotes, please.’

On line 4 we add an action. Think of an action as an instruction to WordPress to do something. Then we have a hook, telling WordPress where we want the something done. Note that we could use any (almost) of the Genesis hooks here. Want your piece of content to appear after your footer (maybe it’s not a very important message after all)? Use the genesis_after_footer hook. The last part tells WordPress what the something is we want it to do: in this case, run the function_name function.

Check out this screenshot of your function/hook combo in action:

function

Pretty nifty, huh?

Further watching

I just found this most awesome video introduction to hooks in Genesis. It looks like there are lots of other useful videos too!

Leave a Reply

Your email address will not be published. Required fields are marked *