Hooks, Filters, and Actions in WordPress: How they work and their importance.

Hook, Filter và Action trong WordPress

The article explains Hooks, Filters, and Actions in WordPress. Hooks are used to edit themes or plugins without touching the core, preventing overwriting when updating. There are two types of Hooks – Filter and Action. Action Hooks perform new actions in themes. Filter Hooks overwrite existing variables or content in themes or plugins. Examples of using Action and Filter Hooks are provided. Using Hooks allows for extensibility in WordPress without modifying core files. This article by Wiki Minh Duy provides a clear explanation of Hooks, Filters, and Actions in WordPress.

When opening files in WordPress like functions.php, we will see the function appear and add_filter(). Have you ever wondered what Hooks, Filters, and Actions are? These are functions that make us a bit confused. But this article will explain clearly and thoroughly what Hooks, Filters, and Actions are.

What are Hooks?

When we want to edit something in a Theme or Plugin, the best way is not to touch the Core. Because if we update, the new code will overwrite. So it will be very time-consuming to edit. A great solution is to use Hooks. When using Hooks, changes will be overwritten when you update that plugin.

There are 2 types of Hooks supported by WordPress:

  • Filter: to change the value of an existing variable by overwriting its contents
  • Action: Perform a new action that has never been performed before

Now you know what Hooks are, right? However, Filters and Actions are still a bit vague. Keep reading to understand more.

See also  How to hide the WordPress admin bar for users

What is an Action Hook function?

Action Hook is one new action never performed on Theme. An action hook no return value is required.

Action Hooks are hooks that are triggered on a specific event or action that occurs in the flow of a particular request in WordPress. One can add their own functionality on such actions and can also perform some additional tasks on that particular action.

For example:
I will write code in the file footer.php as follows:

add_action( 'copyright', function() {
echo '<p>Designed by Wiki Minh Duy</p>';
} );

We will see a connection between these two files. In header.php, we will use the function do_action() to perform an action. And in functions.php, use add_action to add that action and image for the logo.

What is the Filter Hook function?

Filter Hook is to overwrite a variable or something that already exists; it can be in a Plugin or Theme. Now we will edit the PHP script right at the place where that anchor point has been declared.

Filter Hook is an action where we can modify the returned content. That content can be the text of a page or post, the author name, or any other option retrieved from the database. With WordPress Filters, we usually work on the content, make changes, and then return that content.

Filters in WordPress are usually triggered before any content is saved or displayed on the screen. One can add a function on a filter and change what content is saved or displayed on the browser.

Same as do_action, here we have the function apply_filters().

See also  Using Editor in WordPress Plugin for Enhancing Website Development

For example:
At file footer.php often written:

<?php $copyright="Design by Wiki Minh Duy";
echo apply_filters( 'wiki.minhduy_copyright', $copyright );
?>

Now to change that text we go to functions.php and add the code:

<?php function wiki.minhduy_copyright_filter( $content ) {
$content="Copyright by Wiki Minh Duy";
return $content;
}
add_filter( 'wiki.minhduy_copyright', 'wiki.minhduy_copyright_filter' );
?>

Conclusion: Action Hooks and Filter Hooks are core things in WordPress. These make the WordPress platform completely extensible without having to modify any files in the Core. In a Plugin or Theme, you can execute your code on specific actions without having to make modifications to any core files in WordPress.

Rate this post

Related posts