An Introduction to WordPress Hooks

Hooks are a great way to easily modify the theme code without changing the actual source code. In this article we present a few useful hooks available to you in the GavernWP Framework.

WordPress Hooks

We’ve placed three (3) types of hooks in the GavernWP Framework:

  • hooks associated with the document,
  • hooks associated with the content, and
  • hooks associated with the comments.

The full list of these hooks is available here.

It is very simple to use these hooks. Simply create your own function and then set it as a second parameter in the add_action function:

add_action('HOOK_NAME', 'YOUR_FUNCTION_NAME');

We recommend placing all hooks in the gavern/hooks.php file.

Take a look at some of the interesing hooks:

  • gavernwp_fonts – useful when you need to add your own fonts code in the head section
  • gavernwp_doctype – use this hook to change the page doctype
  • gavernwp_head – useful to add something at the end of the head of the page
  • gavernwp_footer – the same as gavernwp_head, but adds your code before the closing body tag
  • gavernwp_body_attributes and gavernwp_html_attributes – if you need to add your own attributes to the body or html elements on your page
  • gavern_wp_title – useful if you need to modify the page title
  • gavern_before_sidebar and gavern_after_sidebar – very useful hooks if you need to add some code before or after the all sidebars with widgets.

GavernWP supports around 30 hooks and this number will increase (especially with version 1.7).

What’s more, you can also add your own hook to the theme’s source code. The creation of new hooks is really simple – use simple code similar to the following:

do_action('YOUR_NEW_HOOK_NAME');

That’s all! This code creates a new hook which can be used in your code and will be executed with the do_action function.

Feel free to test this tip with our free WordPress Theme – Meet GavernWP.

An Introduction to WordPress Hooks 5.005 (100.00%) 1 vote
Share
This article was first published February 14th, 2013