WP_CLI::add_hook()


目录

Schedule a callback to be executed at a certain point.


Usage

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

Notes

Hooks conceptually are very similar to WordPress actions. WP-CLI hooks are typically called before WordPress is loaded.

WP-CLI hooks include:

The parentheses behind the hook name denote the number of arguments being passed into the hook. For such hooks, the callback should return the first argument again, making them work like a WP filter.

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 ( $name ) {
       if ( !is_multisite() ) {
           WP_CLI::error( 'This is not a multisite installation.' );
       }
   }
) );

Internal API documentation is generated from the WP-CLI codebase on every release. To suggest improvements, please submit a pull request.



版本号 #1
由 cocobook-sync 创建于 2026-10-28 11:39:29 UTC
由 cocobook-sync 更新于 2026-10-28 11:39:29 UTC