WordPress Hooks Actions and Filters Introduction Part -1

What are hooks?

Hooks are generic term is WordPress. That allows you’r plugin to hook into the rest of the WordPress by calling your custom functions at a specific time.

There are different points in WordPress where we have been given hooks and with these hooks we can attach our custom functions later on. Let’s say there is a developer working on an existing theme and if he wants to insert his own codes or if he wants to modify data. It will be possible with custom function hooks.

Why do we need Hooks?

Hooks are important so that we don’t mess with the core. Let’s say you go ahead an add your own codes and in future if there is been an update in WordPress you might end up loosing all the changes or messing up with the core.

Second is that we can modify and add new functions to WordPress core. You can even modify the existing functionality using hooks.

Types of Hooks

  • Action
  • Filter

Action: Action is a php function that is executed at a specific point throughout the WordPress core.

Example:

  • Creating a Widget when WordPress in initialising.
  • Sending a tweet when someone publishes a post.

Filter: Filter allows you to get and modify WordPress data before it it send to database/Browser using our custom function.

Example:

  • Customising how excerpts are displayed.
  • Modifying your site title before it is displayed.

There are many more uses and benefits. These are the one’s top of my head.

By this point i thing we should what is the difference between Action and Filter.

Actions allow us to run our code at a specific time when WordPress is doing something.

Filters give us access to data which we can modify and it lets us return that data back with our custom function.

This means action will allow you to insert certain code in WordPress wherein filter will allow to get access to any particular data in form of a Variable and you can latch on to that hook, go ahead and modify that data. Which will further go ahead and return the new value of the variable which would be given back to that hook for display.

As we already know there are two types of hooks. which are action and filter hooks. Let’s see. The below mentioned table will explain what exactly it does.

Action

add_action() : Hooks a function on to a specific action. ( It lets you extend the functionality plus lets you add you custom functions without messing the core)

do_action() : Execute functions hooked on a specific action hook. ( It allows you to access the hooks and execute the custom functions)

Filter

add_filter() : Hook a function or method to a specific filter action.( It hooks the custom function)

apply_filters() : Calls the callback functions that have been added to a filter hook.(It executes the hooked custom function at a given time)

Let us check the in detailed information about these action and filter.

do_action($tag, $arg)

do_action( $tag, $arg)

WordPress in its core has given us do_action() at several points. So that if you have to add your own set of code it should be possible. do-action( ) accepts several parameters. One of them is $tag(name of the hook) and the other is $arg(arguments). There can be single argument or there can be multiple arguments as well. And we. can turn one of these arguments into a function that is why they are there.

add_action( $tag, $cust_func_name, $priority, $arg_count) 

function cust_func_name ($arg) {
Implement your changes here
}

The action hook lets us add our own custom functionality. So all you have to do is find out the name of the specific hook which will allow us to add our own set of codes in that point. Once we get the hook all we have to do is go to our plugins or themes functions.php file and define a function called add_action( ). Add the name of the hook $tag. Which of course has to be same as do_action(). We give the name of the function $cust_func_name(Its a made up name. It can be anything you want). $priority defines at what point will the function execute. Well $priority is an integer hence by default it is 10. We can have multiple functions attached to the same hook. So how do we decide which one is to be executed first. The lower the priority number the sooner it will be executed. If two functions have the same priority number the one which is defined first will be executed first.This is where $priority comes in picture. $arg_count by default is one. As we already know do_action() can have multiple arguments. You need to mention the number of arguments you want to use.

The same amount of argument needs to pass in function as well. So if you want to use two arguments out of three. Than you add two arguments into the custom function.

$Value = apply_filters($tag, $value, $arg)

add_filter($tag, $cust_func_name, $priority, $arg_count) {
<!-- modify $value -->
<!-- Return $value -->
}

apply_filters() are similar to the do action except that it also give us an access to the $value. Which is basically WordPress data which we can be used in our custom function. We can modify that data and we can return it back to the apply_filters(). apply_filters() will return the value back to the browser. So if you want to modify your own data just add these custom functions to your functions.php file of your theme or plugin. Make sure to mention the same hook name($tag), Custom function name($cust_func_name) along with the $priority with $arg_count.

If you realise the add_filter() and add_action accept the same amount of parameters but whats different is that we will have access to the value into our function.

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: