WordPress: Debug Functions Attached To A Filter

I was debugging something in WordPress, trying to figure out what callback function was applying a change to a filter (in this case a post’s content). To get a list of what functions were hooked into the filter, I threw together a quick helper function and I thought I’d post it here incase anyone else found it useful.

function viper_debug_filter( $filter ) {
	add_filter( $filter, function( $value ) {
		global $wp_filter;

		$filters = array();

		foreach ( (array) $wp_filter[ current_filter() ] as $priority => $functions ) {
			foreach ( (array) $functions as $function => $args ) {
				$filters[$priority][] = $function;
			}
		}

		var_dump( current_filter(), $filters );

		return $value;
	} );
}

// Pass the filter name here
viper_debug_filter( 'the_content' );

Nothing fancy like excluding itself from the output — it was just a quick and dirty hack. Use it as you see fit.

2 thoughts on “WordPress: Debug Functions Attached To A Filter

Comments are closed.