Any Questions?

024 7693 3959

What are WordPress Hooks (WP Series Part 3)

Wordpress What Are Hooks

Firstly welcome back. In the last tutorial I covered how to create a simple WordPress plugin. Before we go any further it is incredibly important that we do not ignore WordPress hooks. Changing WordPress core files and functionality is frowned upon massively, so how do we go about manipulating core functionality. This is where WordPress hooks come into the equation. Hooks allow developers to run code at specific points. WordPress has hooks embedded throughout its code, which enables developers to call specific functionality at the correct point.

Filters and Actions

WordPress has two main types of hooks.

  • Actions
  • Filters

What are actions?

Actions allow developers to run functionality when WordPress does something that has a hook. A good example of this is enqueueing scripts into your head. Consider the following code.

    function our_scripts(){

    add_action('wp_enqueue_scripts', 'our_scripts');

If you have spent any time developing themes with WordPress you should have came across enqueueing scripts in the past.We have firstly created our function our_scripts which contains all of our scripts and styles. Once enqueued we need to call the function to run along with any other enqueued scripts. This is where we use the WordPress function add_action(). By using this function we define both the function we would like to run as well as when to run to run the function.

What are filters

Filters allow developers to manipulate the data WordPress provides hooks for. For example lets say we would like to edit the default content on a page to include the text “This is an example of a filter” at the end of the article.

    function change_content($content){

            $amend_content = 'This is an example of a filter';
            $content .= $amend_content;

       return $content;


    add_filter('the_content', 'change_content');

The same contents in the example before are being used in the above example. We are simply hooking into and existing hook but this time we are using add_filter() rather than add_action(). We ensure that we have passed the $content object through our function, ran a basic conditional check on whether the page exists, amended our content and then returned it.

We use add_filter() the same way we did with add_action(), we are defining the hook we would like to hook into and then calling the function we would like to use instead.

There are hundreds of different WordPress hooks available to WordPress developers. I would recommend visiting WordPress Codex or for an extensive list of hooks.

Now we have a basic understanding of how both the hooks add_filter() and add_action() work, we will be moving onto creating a settings page for our plugin in the next tutorial. If you have any questions please feel free to comment.

Written by:

Creative Director - - 07541987924 - DOB: 02/1993/27

With over five years commercial experience in the web industry, Adam specialises in all things web related. Adam has a passion for CRO, ensuring that the websites he creates don't just look great but also make his clients money.

Mash Web Design