WordPress Theme Hooks

May 12, 2014

 - Tags: ,

In a previous article, I talked about WordPress hooks and filters, and how they are used to enrich or modify WordPress. Theme hooks are a bit different from standard hooks. While standard hooks are defined in the WordPress core code, theme hooks are defined in the themes themselves and can be created on-demand for a specific need.

Example of Theme Hook

In Notifications is a plugin which displays notifications at the top of the screen:
theme-hooks_notification

the developer needed to insert the html code of the notification right after the <body>. Unfortunately, there isn’t any WordPress hook for this. The plugin developer had to create a custom hook in the theme, or a “Theme Hook”:
do_action( 'tha_body_top' );
Since it’s not a hook defined in the core code of WordPress, anyone who wants to use this plugin would need to paste the previous piece of code right after the <body> opening tag:

<body>
do_action( 'tha_body_top' );

For end users, it would not be very convenient to copy-paste this piece of code in all the PHP files in order to use this plugin. Instead, it would be better if the end user could choose a theme already implementing this theme hook for us. This takes us to the next paragraph: “The Theme Hook Alliance”

The Theme Hook Alliance

The Theme Hook Alliance is a WordPress project for standardizing theme hooks. It proposes a standard for implementing theme hooks. If you use a plugin that requires some hooks defined in this standard, you can either insert the hooks yourself in the php templates or choose a theme that enforces the Theme Hook Alliance standard (these themes can be found with a “Theme Hook Alliance” tag in the WordPress directory). Of course, in an ideal world there would just be hooks in the WordPress Core. That’s why The Theme Hook Alliance hopes to get enough traction so some of these hooks will get implemented in WordPress Core.

Conclusion

In a previous article, we saw hooks and filters, important elements of the core code of WordPress. In this article, I explained how we can create our own custom hooks to help code new features. Finally, I talked about the Theme hook alliance, a WordPress project to standardize theme hooks.

If you liked the article, you can subscribe to my blog to receive updates when new articles are posted.

If you would like to know more about Theme hooks, please check out the below resources.

Additional Resources

  • The official page of the Theme Hook Alliance on github: Theme Hook Alliance
  • A theme implementing standard of the Theme Hook Alliance : Museum Core
  • Another theme implementing this standard: Oenologye
  • An interesting article about the Theme Hook Alliance on WPTavern: Museum Core

Leave a Reply