Get all callbacks for a hook in WordPress

/**
 * Gets all callbacks for a hook.
 *
 * @see https://wpcodebook.com/wordpress-get-hook-callbacks/
 * @see https://developer.wordpress.org/reference/classes/wp_hook/
 * @see https://stackoverflow.com/questions/5224209/wordpress-how-do-i-get-all-the-registered-functions-for-the-content-filter
 */
function wpcodebook_hook_callbacks( $hook ) {
	global $wp_filter;
	return $wp_filter[ $hook ]->callbacks ?? array();
}

Example

Print all callbacks for the the_content filter:

$callbacks = wpcodebook_hook_callbacks( 'the_content' );

echo '<pre>';
foreach ( $callbacks as $priority => $_callbacks ) {
	foreach ( $_callbacks as $callback_id => $callback_data ) {
		$callback = $callback_id;
		if ( isset( $callback_data['function'] ) ) {
			if ( is_array( $callback_data['function'] ) ) {
				$callback = sprintf( '%s::%s',
					get_class( $callback_data['function'][0] ),
					$callback_data['function'][1]
				);
			} else {
				$callback = $callback_data['function'];
			}
		}
		echo "{$callback}() [{$priority}]" . PHP_EOL;
	}
}
echo '</pre>';

Output (in the function [priority] format):

WP_Embed::run_shortcode() [8]
WP_Embed::autoembed() [8]
do_blocks() [9]
wptexturize() [10]
wpautop() [10]
shortcode_unautop() [10]
prepend_attachment() [10]
wp_replace_insecure_home_url() [10]
capital_P_dangit() [11]
do_shortcode() [11]
wp_filter_content_tags() [12]
convert_smilies() [20]

Leave a Comment