Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Hook or Be Hooked #wctoga

Hook or Be Hooked #wctoga

Talk from WordCamp Saratoga

Jonathan Christopher

November 21, 2015
Tweet

More Decks by Jonathan Christopher

Other Decks in Technology

Transcript

  1. Hook or Be Hooked! Using Hooks (Actions & Filters) in

    WordPress Jonathan Christopher Co-owner, Iron to Iron @jchristopher
  2. • Customizing code is awesome • Open source begs to

    be customized • Extendable software creates possiblities • WordPress has been designed for proper extendability through APIs Software Extensibility What? Why? How?
  3. What are Hooks? As defined by The Codex: A Hook

    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
  4. –Jonathan Christopher “Hooks are events that occur during a request.

    As a developer you can use these hooks 
 to implement your own customizations 
 without modifying existing core code.”
  5. Plugin: “WordPress, I’d like to do something”
 WordPress: “Okay, noted!”


    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!”
  6. Using add_action() or add_filter() your code is added to a

    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
  7. Hooks help you to not modify core Editing core files

    (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
  8. Actions Actions are arbitrary points throughout the execution of a

    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
  9. • Small things, big things, & everything in between •

    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?
  10. A hook begins with the function you want to fire.

    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
  11. Anatomy of a Hook add_action() The call to the WordPress

    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 );
  12. If you’ve ever built a plugin or theme, you’ve probably

    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
  13. You can also use Hooks within classes Hooks in action

    <?php class MyClass { function __construct() { add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); } function enqueue_scripts() { wp_enqueue_script( 'jquery' ); } } new MyClass();
  14. Why? If you’re writing code to publicly release, add Hooks

    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?
  15. Filters Filters allow developers to customize variables Adding Hooks to

    your own code Why? When? How? $args = array( 'param_1' => true, 'param_2' => true, ); $args = apply_filters( 'my_filter_tag', $args, $arg2, $etc ); do_something( $args );
  16. Filters Filters allow developers to customize variables Adding Hooks to

    your code Why? When? How? $args = array( 'param_1' => true, 'param_2' => false, ); $args = apply_filters( 'my_filter_tag', $args, $arg2, $etc ); do_something( $args );
  17. How you can utilize that filter Utilizing a Filter <?php

    function my_filter_tag_callback( $args ) { $args['param_1'] = false; return $args; } add_filter( 'my_filter_tag', ‘my_filter_tag_callback' );
  18. How you can utilize that filter Utilizing a Filter <?php

    function my_filter_tag_callback( $args, $arg2, $etc ) { if ( 'foo' == $arg2 ) { $args['param_1'] = false; } return $args; } add_filter( 'my_filter_tag', 'my_filter_tag_callback', 10, 3 );
  19. How you can utilize that filter Utilizing a Filter <?php

    function my_filter_tag_callback( $args, $arg2, $etc ) { if ( 'foo' == $arg2 ) { $args['param_1'] = false; } return $args; } add_filter( 'my_filter_tag', 'my_filter_tag_callback', 10, 3 );
  20. Actions Put yourself in the shoes of another developer:
 Would

    anyone want to do something at this point? Adding Hooks to your own code Why? When? How? do_action( 'my_action_tag', $arg1, $arg2, $etc );
  21. WordPress Core Hook documentation for core is awesome:
 http://codex.wordpress.org/Plugin_API/Action_Reference
 http://codex.wordpress.org/Plugin_API/Filter_Reference

    Plugins & Themes Should be included in relevant documentation. You can also dig through the code itself. How do you find Hooks?
  22. Adjacent Hook Functionality did_action() Retrieve the number of times an

    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
  23. Adjacent Hook Functionality __return_true add_filter( 'my_filter_tag', '__return_true' ); __return_false add_filter(

    'my_filter_tag', '__return_false' ); __return_zero add_filter( 'my_filter_tag', '__return_zero' ); __return_empty_array add_filter( 'my_filter_tag', ‘__return_empty_array' ); __return_null add_filter( 'my_filter_tag', '__return_null' ); __return_empty_string add_filter( 'my_filter_tag', ‘__return_empty_string' ); Save time with utility functions for Filter return values
  24. • Always return the same data type • Use an

    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