+91-943-185-6038 me@shashidharkumar.com

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:
1) https://codex.wordpress.org/Plugin_API
2) https://www.tipsandtricks-hq.com/wordpress-action-hooks-and-filter-hooks-an-introduction-4163
3) http://code.tutsplus.com/articles/the-beginners-guide-to-wordpress-actions-and-filters–wp-27373
4) http://blog.teamtreehouse.com/hooks-wordpress-actions-filters-examples

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.

See also  How to use WooCommerce's default price at any custom template