2 Powerfull Hooks in WordPress

2 Powerfull Hooks in WordPress

Hooks are a way for one piece of code to override other code at specific, pre-defined place. They make up the foundation stone for how plugins and themes interact with WordPress Core, but they’re also used extensively by WordPress Core itself.

There are two types of hooks: Actions and Filters. To use them, we need to function to them , and then register it with a WordPress hook for a specific actions or filters.

WordPress Actions  allow you to add data or change how WordPress operates. Actions will run at a specific point in the execution of WordPress Core, plugins, and themes. Functions for Actions can perform some kind of a task, like echoing output to the user or inserting something into the database. Action hook function do not return anything back to the calling Action hook.

function customPostFunction( $post_id ) {
    //do something
add_action( 'save_post','customPostFunction' );

Actions can also be used to handle ajax request with GET and POST Method.

add_action("wp_ajax_custom_contact_form", "contactFormHanlde");

function contactFormHanlde () {
   // Do anything with $_POST and $_GET request

WordPress Filters give us the ability to change data during the execution of WordPress Core, plugins, and themes. Callback functions for Filters will accept a variable, modify it, and return it. They are meant to work in an isolated manner, and should never have side effects such as affecting global variables and output. Filters expect to have something returned back to them.

echo apply_filters( 'filter_name','filter_variable' );

WordPress provides many hooks that you can use, but you can also create your own so that other developers can extend and modify your plugin or theme.

Further, you can use the add_filter() function to create a custom filter. You define the name of the filter you want to call, along with the function that will be called to modify the filter.

function modify_value( $filter ) {
    //change $filter and return new value
add_filter( 'original_filter','modify_value' );

The main difference between an action and a filter can be summed up like this:

  • an action takes the info it receives, does something with it, and returns nothing. In other words: it acts on something and then exits, returning nothing back to the calling hook.
  • a filter takes the info it receives, modifies it somehow, and returns it. In other words: it filters something and passes it back to the hook for further use.

Said another way:

  • an action interrupts the code flow to do something, and then returns back to the normal flow without modifying anything;
  • a filter is used to modify something in a specific way so that the modification is then used by code later on.

The something referred to is the parameter list sent via the hook definition. More on this in later sections.

Top ↑

Leave a Reply

Let's talk

If you want to get a free consultation without any obligations, fill in the form below and we'll get in touch with you.