Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 

Hooks

Overview

Hooks are a way for us to add our own code to customize default WordPress behavior. There're two types of hooks in WordPress:

  • Action Hooks let us run our own code when a certain event takes place in the WordPress lifecycle.
  • Filter Hooks let us get data from WordPress, modify it, and then return it back customized.


Important Action Hooks Functions

There are three fundamental functions to know about when working with action hooks:

<?php // index.php
  do_action( 'before_footer' ); // create an action hook
?>

<?php // functions.php
  add_action( 'before_footer', 'my_function' ); // hook in code
  remove_action( 'before_footer', 'my_function' ); // unhook code
?>
  • Wherever do_action() occurs, we have an action hook. The function doesn't do anything by itself, but it's the hook that we can use to run.
  • The add_action() function lets us add a function to an action hook.
  • To remove an action, we have to call remove_action().


WordPress Actions Runtime Lifecycle

The Action Lifecycle refers to the order and relationship od do_action() calls. Check Plugin API/Action Reference | WordPress Codex for more information.



Explore Action Hooks

There're several ways to explore action hooks:

  1. Use the global variables $wp_actions and $wp_filter to get a list of all the action hooks and filters.
    • $wp_actions is an array of all the action hooks that have been registered.
    • $wp_filter is an array of all the filters that have been registered.
  2. Use the r-debug.php plugin to get a list of all the action hooks and filters.
    • Add require( dirname( __FILE__ ) . '/lib/r-debug.php' ); to the functions.php file.
  3. Use the Debug Bar and Debug Bar Actions and Filters plugins to get a list of all the action hooks and filters.
  4. Use the Simply Show Hooks plugin to get a list of all the action hooks and filters.