WordPress Managed Hosting - 40% Off For 4 Months - Coupon Code: BFCM2021 Avail Now

WordPress Hooks – Everything You Need to Know

Ibad Rehman — August 21, 2019 3 Minutes Read

Here’s a question a lot of WordPress beginners might ask: what are WordPress Hooks, and what can they be used for?

Well, WordPress is a highly customizable CMS that allows its users to make changes according to their specifications. Some users customize their WordPress website using just the Appearance functionality while the rest add their custom functionality by using WordPress Hooks.

In this guide, I will explain what hooks are in WordPress and how they can be used to modify or create a specific functionality in WordPress.

What are WordPress Hooks?

WordPress Codex defines hooks as a bridge through which WordPress themes and plugins interact with the WordPress core. The functionality of hooks allows the developers to connect their custom code with the default functions present inside the WordPress core.

It’s safe to say that hooks are custom PHP functions that can be used or hooked to alter the default WordPress behaviour without affecting the core files. Hooks allow WordPress to interact with the custom code present inside plugins and functions.php file.

There are two types of hooks; Action Hooks and Filters Hooks.

Action Hooks

Action hooks are executed at a specific time to perform a certain task like displaying an output or adding data to the database. Action hooks do not return any value and they only perform a certain task when called upon.

For example; create_category function runs whenever a new category is created and does not return anything.

Filters Hooks

Filters Hooks, on the other hand, allow you to change the data during the execution of WordPress. Functions hooked to Filters accept variables and return a value. Some Filters also accept more than one argument.

For example; add_filter(‘the_title’, ‘strrev’); accepts the string as an argument and returns the reverse of that string.

Filters are used to handle a specific task and they can modify the data and always return something at the end.

How WordPress Hooks Are Used?

Now that we have understood the basic concepts of WordPress hooks, let’s move ahead and see both Action and Filters in action.

Action Hooks Example

There are more than 200 hooks available so covering them all is not a good idea. However, to get you started, I’m going to show you a simple example and then I’ll explain what I’ve done.

Example 1: Custom Menu in the Admin Area

Let’s examine the code above by going line-by-line.

In the first line of the code, I gave a name to my function. In the second line, I am calling another function with a few arguments inside. Finally, I attached my custom function with an Action hook of ‘admin_menu’ which will run my function whenever the admin menu is being generated.

Example 2: Adding Custom Text After the Header

In the code above, if the current page is not the home page then the function won’t display the custom text. Which basically means that the custom text will only show on your website’s homepage.

At the end of the code snippet, I attached my custom code to an Action hook ‘__after_header’.

Similarly, you can experiment with other Action hooks and create your custom functionality.

Filter Hooks Example

Unlike Action hooks, Filters accept arguments and return a value. Let’s look at the examples and see how they can be used.

Example 1: Changing the Excerpt Length

In the code above, I take advantage of one of the WordPress Filters ‘excerpt_length’ and hook it with my custom code which returns the numeric value of 15 for my new excerpt length.

Example 2: Change Title

In this simple example, I simply defined a custom function for changing the post title. Later, I hooked it to a filter ‘the_title’.

Wrapping It Up!

WordPress hooks are useful in connecting custom functionality with your WordPress’s core features. In this guide, we saw how Action and Filter hooks are different and how they can be used at various places inside our WordPress sites.

If you have any questions, or if you’re confused about any concept in the article, let me know in the comments section below and I’ll get back to you!

Create Faster WordPress Websites!

Free eBook on WordPress Performance right in your inbox.


    Create Faster WordPress Websites!

    Free eBook on WordPress Performance right in your inbox.

      Ibad Ur Rehman is a WordPress Community Manager at Cloudways. He likes to explore latest open-source technologies and to interact with different communities. In his free time he likes to read, watch a series or fly his favorite Cessna 172SP in X Plane 10 flight simulator.

      THERE'S MORE TO READ

      Newsletter

        WordPress Help Zone - Ultimate WordPress Pit-Stop

        Learning WordPress? Or are you expert enough to help others? Join our WP Facebook group!