Hooks API


The Customizr theme is entirely built on an extensible filter and action hooks API, which makes customizations easy as breeze, without ever needing to modify the core structure. In other words, Customizr’s code acts like a collection of plugins that can be enabled, disabled or extended.

As of today, there are 367+ hooks (filters and actions) available for developers in the theme.

If you are not comfortable with the WordPress hooks API, you may want to read this excellent beginner’s guide on WordPress hooks written by Electricfeet. More advanced developers will find many working examples of hooks implementation in the Customizr code snippets section of this website.

Note : remember to always use a child theme when you add custom functions to a WordPress theme.

How to use the Customizr hooks API ?

When you really get the concept of filter and action hooks in WordPress, there’s not limit to what you can do. This is how this CMS has been built, around this hook concept. (just look the core code, they are everywhere!), and this is also why it is so popular : hooks make the code very easily extensible, modular and clean.

Now how to use them ?

Instead of re-explaining again the WordPress hooks concept or review the whole WordPress hooks function reference, I will rather use a simple example here.

Note : the code example below has to be pasted in your functions.php file.

 

In the Customizr theme, the header is rendered with functions hooked to one single action hook named : ‘__header’, while the footer is displayed with functions hooked to the action hook named ‘__footer’.

How to remove ( unhook ) all those actions ? With a function called remove_all_actions().

The following code will remove all actions of __header and __footer when displaying a 404 error page.

Note : As you can see in the snippet above, the hooks are set in the template_redirect action. I could have simply written the following code in functions.php :

But of course this would have removed the header and footer in all contexts : home, page, single posts,….

If you need to use conditional tags in WordPress (like is_page(), is_home(), is_single(), is_search(), is_404() ) , be aware that most of them use the global $wp_query object. This $wp_query object is generated by WordPress in every pages but bear in mind that the functions.php file is called before the $wp_query is fully built.

In other words, if you write conditional tags in your functions.php file, they will just not work if they are not fire after the $wp_query has been built.

That’s where the hook named template_redirect is useful. The template_redirect hook is run when the $wp_query has been built, you can then use conditional_tags in this hook.

More examples : check the code snippet section.

 

 

Structural actions hooks

General Customizr action hooks structure

 

Header action hooks structure

 

Left

 Right