Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I keep coming across this term hooks in various programming articles. However I don't understand what they are, and how can they be used. So I just wanted to know what is the concept of hooks; if someone could link me to some examples, particularly in the context of web development, it would be great.

share|improve this question

closed as too broad by gnat, MichaelT, Bart van Ingen Schenau, GlenH7, ChrisF Jan 6 '14 at 21:05

There are either too many possible answers, or good answers would be too long for this format. Please add details to narrow the answer set or to isolate an issue that can be answered in a few paragraphs.If this question can be reworded to fit the rules in the help center, please edit the question.

up vote 16 down vote accepted

My answer pertains to WordPress which is written in PHP, but this is a general development mechanic so it shouldn't really matter, despite the fact that you put 'python' in your question title.

One good example of usage of hooks, coincidentally in web development, are WordPress' hooks.

They are named appropriately in that they allow a way to 'hook into' certain points of the execution of a program.

So for example, the wp_head is an 'action' that is emitted when a WordPress theme is being rendered and it's at the part where it renders the part that's within the <head> tags. Say that you want to write a plugin that requires an additional stylesheet, script, or something that would normally go within those tags. You can 'hook into' this action by defining a function to be called when this action is emitted. Something like:

add_action('wp_head', 'your_function');

your_function() could be something as simple as:

function your_function() {
    echo '<link rel="stylesheet" type="text/css" href="lol.css" />';

Now, when WordPress emits this action by doing something like do_action('wp_head');, it will see that your_function() was 'hooked into' that action, so it will call that function (and pass it any arguments if it takes any, as defined in the documentation for any particular hook).

Long story short: It allows you to add additional functionality at specific points of the execution of a program by 'hooking into' those points, in most cases by assigning a function callback.

share|improve this answer

Depending on the background of the person asking sometimes the answer to this question can be as simple as hooks are usually another word for an event handler, which Blaenk gave an excellent explanation for.

There are also uses of this phrase that are when you inject an event or code into a process that otherwise does not have events like moles for example.

share|improve this answer
Thanks Bill,moles looks like a .NET concept , which is totally alien to me, could you please point me to something pythonic or php or something more general. – Rasmus Oct 23 '10 at 3:07
Unfortunately I don't use those two enough to claim expertise, but if I am reading it correctly I believe… is a similar idea being discussed but moles can hook a compiled assembly. A miniport driver is another place the hook phrase is used sometimes. My point was more that the "hook" terminology can be used another way sometimes,in older MS tech anyway, perhaps the second use does not apply to python as well. – Bill Oct 23 '10 at 4:28

Not the answer you're looking for? Browse other questions tagged or ask your own question.