WP_CLI::add_hook PHP Method

add_hook() public static method

Hooks conceptually are very similar to WordPress actions. WP-CLI hooks are typically called before WordPress is loaded. WP-CLI hooks include: * before_invoke: - Just before a command is invoked. * after_invoke: - Just after a command is involved. * 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. WP-CLI commands can create their own hooks with WP_CLI::do_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 install.' ); } } ) );
public static add_hook ( string $when, mixed $callback ) : null
$when string Identifier for the hook.
$callback mixed Callback to execute when hook is called.
return null
    public static function add_hook($when, $callback)
    {
        if (in_array($when, self::$hooks_passed)) {
            call_user_func($callback);
        }
        self::$hooks[$when][] = $callback;
    }

Usage Example

Esempio n. 1
0
<?php

/**
 * Use WP-API at the command line.
 */
require_once __DIR__ . '/inc/RestCommand.php';
require_once __DIR__ . '/inc/Runner.php';
if (class_exists('WP_CLI')) {
    \WP_REST_CLI\Runner::load_remote_commands();
    WP_CLI::add_hook('after_wp_load', '\\WP_REST_CLI\\Runner::after_wp_load');
}
All Usage Examples Of WP_CLI::add_hook