Wordpress Hooks

WordPress Hooks: Actions And Filters With Examples

Posted by

If you are a WordPress Developer or want to learn about WordPress to develop a Plugin or Theme, then you should get to know about WordPress hooks.

If you do not want to become a complete developer, but if you want to change something with programming on the site, then you must also know the WordPress hooks.

In this article, we will learn the following topics:

What are WordPress Hooks

A whole WordPress site is made up of functions and database query, the theme and plugin are working together with WordPress to output the text, images, styles, and other files. All of those are interpreted by the browser and put together into a single web page.

The hook is a generic term in WordPress which is used to change the core functionality of WordPress and Plugins without touching the core files and you can write your own hook.

There are two types of hooks in WordPress: Action and Filter.

Action Hook

Action hooks let you perform the custom functions called actions.

It allows you to add extra functionality to the WordPress core and the theme. You can achieve by writing your own function in the action hook.

The following is the syntax for writing action hook:

Suppose you are deleting the user and want to send an email as notification that “your account has been deleted”. For this, you can apply an action hook.

The following are the steps for applying the action hook:

  1. First, open function.php of your activated theme which will be located on the “/wp-content/themes/your-theme” path.
  2. Add the following lines of the code into the function.php. If mail set up is working on your server then it will send an email to the user.

For your reference, you can check the link.

Filter Hook

Filter hooks are also WordPress hooks that manipulate the text and other output.

Suppose you want to add social media links to the end of all posts and if the user clicks on that social media link then that page becomes open.

  1. First, you have to open function.php of your running theme which will be located on “/wp-content/themes/your-theme”.
  2. Add the following lines of code into the function.php:

For your reference, you can check the link.

Removing Filter and Action Hooks

It is very easy to remove the hooks that you have attached to WordPress. It is the opposite way to add hooks, which has a very similar syntax:

The “priority” argument is optional.

Different between action and filter hooks

Action Hook Filter Hook
Getting new things done. Ex. adding CSS and JS. It works on that are already in WordPress. Some things to add or remove. Suppose you add text after the post content.
An Action hook does not need to return value. It is required to return a value.
An Action hook does not need to return value. The hooked function gets a specific value. It does

Leave a Reply

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