WP_CLI::add_hook() Edit

Schedule a callback to be executed at a certain point.

Usage Usage

WP_CLI::add_hook( $when, $callback )
$when (string) Identifier for the hook.
$callback (mixed) Callback to execute when hook is called.
@return (null)

Top ↑

Notes Notes

HooksHooks In WordPress theme and development, hooks are functions that can be applied to an action or a Filter in WordPress. Actions are functions performed when a certain event occurs in WordPress. Filters allow you to modify certain functions. Arguments used to hook both filters and actions look the same. conceptually are very similar to WordPress actions. WP-CLIWP-CLI WP-CLI is the Command Line Interface for WordPress, used to do administrative and development tasks in a programmatic way. The project page is http://wp-cli.org/ https://make.wordpress.org/cli/ hooks
are typically called before WordPress is loaded.

WP-CLI hooks include:

  • before_add_command:<command> – Before the command is added.
  • after_add_command:<command> – After the command was added.
  • before_invoke:<command> – Just before a command is invoked.
  • after_invoke:<command> – Just after a command is invoked.
  • find_command_to_run_pre – Just before WP-CLI finds the command to run.
  • before_wp_load – Just before the WP load process begins.
  • before_wp_config_load – After wp-config.php has been located.
  • after_wp_config_load – After wp-config.php has been loaded into scope.
  • after_wp_load – Just after the WP load process has completed.
  • before_run_command – Just before the command is executed.

WP-CLI commands can create their own hooks with WP_CLI::do_hook().

If additional arguments are passed through the WP_CLI::do_hook() call,
these will be passed on to the callback provided by WP_CLI::add_hook().

# `wp network meta` confirms command is executing in multisite context.
WP_CLI::add_command( 'network meta', 'Network_Meta_Command', array(
   'before_invoke' => function () {
       if ( !is_multisite() ) {
           WP_CLI::error( 'This is not a multisite installation.' );
) );

Internal APIAPI An API or Application Programming Interface is a software intermediary that allows programs to interact with each other and share data in limited, clearly defined ways. documentation is generated from the WP-CLI codebase on every release. To suggest improvements, please submit a pull request.

Top ↑

  • WP_CLI::do_hook() – Execute callbacks registered to a given hook.
  • WP_CLI::add_wp_hook() – Add a callback to a WordPress action or filterFilter Filters are one of the two types of Hooks https://codex.wordpress.org/Plugin_API/Hooks. They provide a way for functions to modify data of other functions. They are the counterpart to Actions. Unlike Actions, filters are meant to work in an isolated manner, and should never have side effects such as affecting global variables and output..
  • WP_CLI::add_command() – Register a command to WP-CLI.

Last updated: