Skip to content

Instantly share code, notes, and snippets.

@danielbachhuber
Last active November 1, 2017 14:54
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save danielbachhuber/0f991d150067d8a332ec to your computer and use it in GitHub Desktop.
Save danielbachhuber/0f991d150067d8a332ec to your computer and use it in GitHub Desktop.
WP-CLI command to list callbacks registered to a given action or filter
<?php
/**
* List callbacks registered to a given action or filter.
*
* <hook>
* : The key for the action or filter.
*
* [--format=<format>]
* : List callbacks as a table, JSON, or CSV. Default: table.
*
* EXAMPLES
*
* wp --require=wp-hook-command.php hook wp_enqueue_script
*/
$hook_command = function( $args, $assoc_args ) {
global $wp_filter;
$assoc_args = array_merge( array(
'format' => 'table',
), $assoc_args );
$hook = $args[0];
if ( ! isset( $wp_filter[ $hook ] ) ) {
WP_CLI::error( "No callbacks specified for {$hook}." );
}
$callbacks_output = array();
foreach( $wp_filter[ $hook ] as $priority => $callbacks ) {
foreach( $callbacks as $callback ) {
if ( is_array( $callback['function'] ) && is_object( $callback['function'][0] ) ) {
$callback['function'] = get_class( $callback['function'][0] ) . '->' . $callback['function'][1];
}
$callbacks_output[] = array(
'function' => $callback['function'],
'priority' => $priority,
'accepted_args' => $callback['accepted_args'],
);
}
}
WP_CLI\Utils\format_items( $assoc_args['format'], $callbacks_output, array( 'function', 'priority', 'accepted_args' ) );
};
WP_CLI::add_command( 'hook', $hook_command );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment