WP_Hook::has_filter() WordPress Method

The WP_Hook::has_filter() method is used to check if a certain filter has been registered with a WordPress hook. This can be useful for checking if a plugin or theme has registered a certain filter, and then taking action accordingly.

WP_Hook::has_filter( string $hook_name = '', callable|string|array|false $callback = false ) #

Checks if a specific callback has been registered for this hook.


Description

When using the $callback argument, this function may return a non-boolean value that evaluates to false (e.g. 0), so use the === operator for testing the return value.


Top ↑

Parameters

$hook_name

(string)(Optional) The name of the filter hook.

Default value: ''

$callback

(callable|string|array|false)(Optional) The callback to check for. This method can be called unconditionally to speculatively check a callback that may or may not exist.

Default value: false


Top ↑

Return

(bool|int) If $callback is omitted, returns boolean for whether the hook has anything registered. When checking a specific function, the priority of that hook is returned, or false if the function is not attached.


Top ↑

Source

File: wp-includes/class-wp-hook.php

	public function has_filter( $hook_name = '', $callback = false ) {
		if ( false === $callback ) {
			return $this->has_filters();
		}

		$function_key = _wp_filter_build_unique_id( $hook_name, $callback, false );

		if ( ! $function_key ) {
			return false;
		}

		foreach ( $this->callbacks as $priority => $callbacks ) {
			if ( isset( $callbacks[ $function_key ] ) ) {
				return $priority;
			}
		}

		return false;
	}


Top ↑

Changelog

Changelog
VersionDescription
4.7.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.