How to use hooks (actions and filters) in wordpress and woocommerce

wordpressWhat are hooks?

Hooks are helpful in WordPress which allow you to manipulate code without editing core files. It is used extensively used throughout WordPress and WooCommerce by developers.

Two types of hook: actions and filters.

Action Hooks allow you to insert custom code.

Filter Hooks allow you to manipulate and return a variable.

There are lot of excellent article on hooks and filters as follows:

Using hooks

If you use a hook to add or manipulate code, you can add your custom code to your theme’s functions.php file.
Action hooks

To execute your own code, your hook used the action hook i.e. do_action(‘action_Name’);. See below a basic example that how to use and where to place code:

add_action(‘action_Name’, ‘my_function_name’);

function my_function_name() {
// My code goes here…

Filter hooks

Filter hooks can be called throughout using apply_filter(‘filter_Name’, $variable); To manipulate the passing variable, you can do as follows:

add_filter(‘filter_Name’, ‘my_function_name’);

function my_function_name( $variable ) {
// My code goes here…
return $variable;

With filters you must return a value.

Like to share it

No comments

Leave a Reply

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