be customized • Extendable software creates possiblities • WordPress has been designed for proper extendability through APIs Software Extensibility What? Why? How?
is an event, i.e. event as understood by the Observer pattern, invoked by the do_action() or apply_filters() call that afterwards triggers all the action or filter functions, previously hooked to that event using add_action() or add_filter(), respectively. Hooks! How WordPress does extensibility
Me: “WordPress, I too want to do something” WordPress: “Sounds good!” … a little while later WordPress: “Okay Plugin & Jonathan, it’s time!” Plugin: “Done!” Me: “Me too!” WordPress: “Okay, time to finish this request!”
canonical queue of function calls Certain Actions and Filters are executed at specific points in time during a request Keep this timing in mind: you cannot use an Action or Filter after it has been executed Hooks! How WordPress does extensibility
(files that come with WordPress or an existing plugin/theme) is bad Using Hooks encourages decoupling Since hooks are standalone pieces of code, they’re inherently modular & cooperative by nature Hook Fundamentals Hooks help you write better code
request that allow you to inject your own function call, potentially making use of available data (or not) Filters Filters are also arbitrary points throughout the execution of a request that allow you to modify data (in the form of a variable) used in the request What are Hooks? Seriously. Hooks come in two flavors: actions & filters
Modify page content, for example: Alter content available in the RSS feed only Build your own theme framework (e.g. Genesis) Customize markup to optimize for SEO based on existing page content Add meta boxes to the WordPress admin, save and retrieve that data • Modify parameters used in logic, for example: Change the number of entries in The Loop Modify HTML classes on elements to help you target with custom CSS Change how many words are used in the_excerpt() • Anything you want, when you want (usually; remember timing) What can Hooks do?
You then use WordPress Hooks API to enqueue your hook which tells WordPress to fire your function when the registered event occurs. Hooks can be standalone functions, closures, or class methods. Anatomy of a Hook
Hooks API: it tells WordPress that we have a Hook we want to add 'hook_tag' The name of the event we want to piggyback (usually referred to as a tag) 'my_custom_function_name' The name of the function you wrote that will execute when the 'hook_tag' action occurs 10 (optional, default: 10) The priority of this specific hook (lower number hooks run before higher number hooks) 2 (optional, default: 1) The number of arguments that get passed to the 'my_custom_function_name' function add_action( 'hook_tag', 'my_custom_function_name', 10, 2 );
used a Hook. Here’s how to use an Action: Hooks in action <?php function my_enqueue_scripts() { wp_enqueue_script( 'jquery' ); } add_action( 'wp_enqueue_scripts', 'my_enqueue_scripts' ); Enqueue jQuery using add_action() and a standalone function
so developers can fully utilize it. When? I like adding Hooks as I develop. If Hooks are missing the code can be less adaptive which means it’s harder for other developers to use it. How? I’m glad you asked! Adding Hooks to your own code Why? When? How?
Action was fired (i.e. did this happen yet?) View in Codex has_action() has_filter() Whether a particular Action/Filter has been used View in Codex remove_action() remove_filter() Remove a specific Hook View in Codex remove_all_actions() remove_all_filters() Remove all Hooks for a specific Action/ Filter View in Codex current_filter() Returns the name of the current Filter View in Codex Some handy functions to keep in mind
appropriate priority • Add to class constructors to ensure placement Best Practices Advice for using Hooks Using • As with functions, use a prefix • Make contextual data available via parameters • Provide documentation Implementing