Home / Blog / Notes / Hook To The Future

Hook To The Future

—

by

FR | EN

Introduction

Hooks are everywhere in WordPress. At the beginning, at every stage, inside functions, around data, and all the way to the end of the execution cycle. They are an integral part of the CMS architecture. There are hundreds of them, and new ones appear regularly.

Hooks provide immense flexibility at the core of WordPress. And by creating your own, you give your code that same ability to adapt.

A living, steerable code. A code that adapts to the present and can even, at times, change the future.

Concept

A hook is an attachment point that WordPress provides so developers can add or modify functionality without touching the core of the CMS.

There are two main types:

  • Actions: to execute code at a specific moment,
  • Filters: to modify data before it is used or displayed.

Hooks are like mounting points built into the engine: they let you attach your own components without disassembling the whole system. They make it possible to connect your own code not only to the WordPress core, but also to any plugins and themes that provide them.

Order

Hooks run one after another all the way to shutdown.

The execution order is crucial. Understanding this sequence is essential: an add_action() or add_filter() that is too early or too late may prevent your code from achieving its purpose.

You can consult the repository of available actions. The first part of the page provides an overview of the loading sequence from muplugins_loaded to shutdown. A similar repository exists for filters as well.

Make sense of WordPress core load – Rarst.net

Contexts

The same hook can appear in multiple contexts.

A hook is never intrinsically “too early” or “too late”; its relevance depends entirely on what you want to accomplish.

The real secret is to always ask yourself: “In which context is this hook being executed?”

This way, a hook is only “too early” or “too late” relative to a specific context, never in an absolute sense.

Some examples to illustrate:

after_setup_theme or template_redirect :

  • after_setup_theme  fires after the theme is loaded. It runs very early, making it useful if you want to register theme features (supports, menus, images, etc.).
  • template_redirect fires before determining which template to load. It only runs once WordPress has resolved which template will be used—making it the perfect moment to redirect a user before anything is displayed.
  • Here, using template_redirect to register a theme feature would be “too late,” and using after_setup_theme to perform a redirect would be “too early.” The main query would not have run yet.

admin_init :

  • admin_init fires during the initialization of an administration page or script.
  • This means that the same hook is shared across multiple contexts (all admin pages).
  • If you want to target only a specific page (e.g., a plugin settings page), you need to add a context check.
  • Here, the hook is neither “too early” nor “too late” in itself: it is simply shared across all screens, and it’s up to you to validate the context.

pre_get_posts :

  • The pre_get_posts hook fires before the execution of every WP_Query request.
  • This hook is implemented in the get_posts() method of the WP_Query class.
    It is therefore executed each time this method runs—that is, every time posts are retrieved via WP_Query.
  • This includes the main query, as well as any secondary queries that may be generated by widgets, menus, or calls to WP_Query. If you want to manipulate only the main query, it is essential to check the context (using is_main_query(), for example).

Timing

To use a hook, it is essential to attach your callback before the hook executes. Otherwise, it simply won’t work, as you can imagine. It also means that as long as a hook hasn’t been executed yet, there’s still time to attach yourself to it — or to the next one.

It also means you can rely on a hook to check a piece of data or a context and plan future actions on hooks that will fire later.

You can register a callback dynamically, on the fly, before the target hook executes, taking the context into account and choosing the right moment.

Mastering the timing of attachment is mastering the flow and behavior of your code.

Branches

A hook is an opportunity to modify or enrich data before it is used. You can rewrite it, redirect it, or adapt it depending on the context, the user, or the moment. It is a bridge between what should have been… and what will be.

Each do_action() or apply_filters() is a key point on WordPress’s timeline: understanding these hooks means knowing when and how to act to influence the flow of data.

Attaching behavior, injecting logic, modifying a value — mastering hooks is the power to change the future of your application.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *