So you’ve probably hearing hooks a lot and its importance for developing WordPress themes and plugins. But what are hooks? And why are they so important. We’ll today we are going to look into what are hooks in WordPress.

Before I being let’s think about hooks in general. When you think about a hook whats the first thing that comes into your mind? My money is probably  this image.Hooks

Hooks are basically a curved or angular piece of metal or other hard substance for catching, pulling, holding, or suspending something. Now you know the basic of what a hook is let’s move on to hooks in WordPress


WordPress Hooks

Like the description above, hooks in WordPress are usually used for holding something, in our case a piece of code.Or in simpler terms

Essentially it’s a place in code that allows you to tap in to a module to either provide different behavior or to react when something happens.


What this means is you can modify any core functionality of a file without touch the file itself. I know it’s a bit confusing, so let me write a code below to help you out. Also don’t think too much about the functions I’m going to use.


add_action( 'my_custom_hook', 'simple_function_for_hook' );
do_action( 'my_custom_hook' );
function simple_function_for_hook() {
	echo 'I am hooked at my_custom_hook';

The above code has hooked a function called simple_function_for_hook into my_custom_hook using add_action()


You might now be asking what’s the big deal anyways, I could easily call my function just by simple_function_for_hook(), you do not have the ability to modify the code for simple_function_for_hook(). With hooks you can not only modify but add your own. Here’s how you can do that

In order to remove it you can simply do

// the function simple_function_for_hook has been removed from hook my_custom_hook
remove_action('my_custom_hook', 'simple_function_for_hook');
add_action('my_custom_hook', 'another_simple_function_for_hook');
function another_simple_function_for_hook(){
echo 'Hi again I am another hook'

I hope you know have a general idea about what Hooks are in WordPress .

In WordPress there are basically two types of Hooks

1.Action hooks (which we used in the example above)

2.Filter hooks

For more information you can have look at the CODEX