Hooks are also known as Filters & Actions which are used by developers to make website changes without modifying the WordPress core files directly. Hooks are used in both theme & plugin files to create & add custom functions or edit & remove functions already in your themes code.
Basically, you can modify the core WordPress code and your themes core files 3 ways:
- Adding functions
- Removing functions
- Editing functions
Rather than make changes to the core WordPress code, hooks allow developers to change the functions of the WordPress core files using custom coding that won’t weaken the core files of WordPress or the theme you have installed.
Each time you update your WordPress installation, your changes will not be lost which would be the case if you edited the WordPress core files.
All custom coding using hooks is placed in your child themes – functions.php file which isn’t effected when you update your plugins or WordPress core files. You will need to save this file before updating your theme.
There are 2 types of Hooks used in WordPress – Filters & Actions: Filters – You can add filters add_filter() to filter out some default functions while leaving other functions active.
- add_filter()
Example – Here the filters remove the header, sidebar and footer which creates a sales or squeeze page from a standard page template.
function custom_remove_defaults($content) {
return false;
}
function my_landing_page() {
if (is_page('007')) {
add_filter('thesis_show_header', 'custom_remove_defaults');
add_filter('thesis_show_sidebars', 'custom_remove_defaults');
add_filter('thesis_show_footer', 'custom_remove_defaults');
}
}
add_action('template_redirect','my_landing_page');
This custom function consists of:
- A function we have created using a descriptive name
- The names of the hooks we want to filter out
- The name of the action we want to execute along with the name of the custom function
This code has been created using Thesis filters so you would need to modify the filter names and page I.D number to make it work with your theme. Actions – You can use Actions to add functions add_action() and also to remove functions remove_action().
Action Hooks provide the location where you can add custom coding like HTML to that specific location which the hook refers to.
- add_action()
Writing Hooks normally happens in your child themes functions.php file located wp-content/themes/child/functions.php
WordPress Hooks
Different themes offer different names for their hooks and even have different locations for the output of the hooks, however the principal is the same.
Note: Canvas Hook/Filter Reference shows you the exact Woo theme locations of where your hooks will be used.
You can see from this Woo hooks map exactly where you can place your custom code using Hooks. Thesis also offer a plugin which creates a Hook Manager like the Woo Hook manager.
- The Thesis Open Hook Plugin is free and an excellent solution for adding any type of content or code anywhere there’s a hook in Thesis.
- The Woo Themes Hook Manager is also another option to add HTML to specific locations around your theme without the need to write code to your themes functions.phpfile.
- The Simple Hooks Plugin for Genesis is a free plugin which basically gives you the same features as the Thesis open hook plugin so you can display any type of content in any location there’s a Genesis hooks.
- First step to using Hooks in WordPress is to add a function which will allow you to add your custom content (normally HTML)
- Second step is to Hook your custom content to the correct location in your theme using the correct hook for that location
Add Actions
Hooks execute the functions you apply to them. All you need to do is add the add_action()command
add_action(‘hook_name’, ‘your_custom_function’);
The Hook name will depend on which theme you are using. Remove Action Removing existing functions from WordPress or your themes functions can be achieved by using the
remove_action(‘hook_name’, ‘function_you_want_to_remove’);
To “hook on” to a function, you’ll need:
- The label of the hook you want to hook onto (for example, `woo_post_after`).
- A function which we’ll use to display your custom content.
- An `add_action()` line to perform the “hooking”.
The best way to learn and master Hooks – Filters and Actions is to setup a test site and try them by referring to the:
- Map of hook locations for your particular theme (Canvas Hook/Filter Reference)
- List of functions you can create using Filters & Actions
I hope this gives you some idea of what a hook is and how you can start learning more about how to use hooks in your WordPress theme.
Was This Tutorial Helpful?