What is: Hooks?

Hooks are the WordPress equivalent of APIs, short pieces of code that interact with another piece of code. The difference, however, is that hooks in WordPress only ever interact inside of the same WordPress installation and not via the internet with a third-party site.

Hooks can request information, modify data, add data, or delete information. In WordPress, hooks can be used with themes, plugins, or to modify the core functionality of WordPress itself.

If you’re new to WordPress, then you won’t have to ever worry about what hooks are or how they function. Hooks, however, are how WordPress developers allow plugins, themes, and other core functionalities to interact with other pieces of code using something called a Callback.

Specifically, hooks in WordPress come in two flavors: actions and filters.

Actions, just as you might expect, are a specific kind of hook that cause something else to happen. For example, if a user on your site leaves a comment, it’s a hook that takes this information and then writes it in the database.

Filters, on the other hand, allow for information to be changed. For example, if you create a new post, your WordPress site may be set up to automatically create a permalink that includes the date that the post was written. This is an example of a filter hook in action.

Perhaps the easiest way to think of an Action is, “If X happens, then do Y.” A filter, on the other hand, is more like, “When X happens, then do Y.”

WordPress comes built-in with a number of hooks, but it is also possible to create your own using the PHP scripting language. The syntax for writing hooks is identical for both actions and filters even though they operate somewhat differently.

The following two tabs change content below.

Jamie Spencer

My name is Jamie Spencer and I have spent the past 10 years building money making blogs. After growing tired of the 9-5, commuting and never seeing my family I decided that I wanted to make some changes and launched my first blog. Since then I have launched lots of successful niche blogs and after selling my survivalist blog I decided to teach other people how to do the same.