has_filter

The timeline below displays how wordpress function has_filter has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 5.9

/**
 * Checks if any filter has been registered for a hook.
 *
 * 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.
 *
 * @since 2.5.0
 *
 * @global WP_Hook[] $wp_filter Stores all of the filters and actions.
 *
 * @param string                      $hook_name The name of the filter hook.
 * @param callable|string|array|false $callback  Optional. The callback to check for.
 *                                               This function can be called unconditionally to speculatively check
 *                                               a callback that may or may not exist. Default false.
 * @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.
 */
function has_filter($hook_name, $callback = false)
{
    global $wp_filter;
    if (!isset($wp_filter[$hook_name])) {
        return false;
    }
    return $wp_filter[$hook_name]->has_filter($hook_name, $callback);
}

WordPress Version: 5.8

/**
 * Checks if any filter has been registered for a hook.
 *
 * 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.
 *
 * @since 2.5.0
 *
 * @global WP_Hook[] $wp_filter Stores all of the filters and actions.
 *
 * @param string         $hook_name The name of the filter hook.
 * @param callable|false $callback  Optional. The callback to check for. Default false.
 * @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.
 */
function has_filter($hook_name, $callback = false)
{
    global $wp_filter;
    if (!isset($wp_filter[$hook_name])) {
        return false;
    }
    return $wp_filter[$hook_name]->has_filter($hook_name, $callback);
}

WordPress Version: 5.7

/**
 * Checks if any filter has been registered for a hook.
 *
 * When using the `$function_to_check` 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.
 *
 * @since 2.5.0
 *
 * @global WP_Hook[] $wp_filter Stores all of the filters and actions.
 *
 * @param string         $tag               The name of the filter hook.
 * @param callable|false $function_to_check Optional. The callback to check for. Default false.
 * @return bool|int If `$function_to_check` 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.
 */
function has_filter($tag, $function_to_check = false)
{
    global $wp_filter;
    if (!isset($wp_filter[$tag])) {
        return false;
    }
    return $wp_filter[$tag]->has_filter($tag, $function_to_check);
}

WordPress Version: 5.6

/**
 * Check if any filter has been registered for a hook.
 *
 * @since 2.5.0
 *
 * @global WP_Hook[] $wp_filter Stores all of the filters and actions.
 *
 * @param string        $tag               The name of the filter hook.
 * @param callable|bool $function_to_check Optional. The callback to check for. Default false.
 * @return false|int If $function_to_check 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. When using the
 *                   $function_to_check 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.
 */
function has_filter($tag, $function_to_check = false)
{
    global $wp_filter;
    if (!isset($wp_filter[$tag])) {
        return false;
    }
    return $wp_filter[$tag]->has_filter($tag, $function_to_check);
}

WordPress Version: 5.4

/**
 * Check if any filter has been registered for a hook.
 *
 * @since 2.5.0
 *
 * @global array $wp_filter Stores all of the filters and actions.
 *
 * @param string        $tag               The name of the filter hook.
 * @param callable|bool $function_to_check Optional. The callback to check for. Default false.
 * @return false|int If $function_to_check 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. When using the
 *                   $function_to_check 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.
 */
function has_filter($tag, $function_to_check = false)
{
    global $wp_filter;
    if (!isset($wp_filter[$tag])) {
        return false;
    }
    return $wp_filter[$tag]->has_filter($tag, $function_to_check);
}

WordPress Version: 4.7

/**
 * Check if any filter has been registered for a hook.
 *
 * @since 2.5.0
 *
 * @global array $wp_filter Stores all of the filters.
 *
 * @param string        $tag               The name of the filter hook.
 * @param callable|bool $function_to_check Optional. The callback to check for. Default false.
 * @return false|int If $function_to_check 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. When using the
 *                   $function_to_check 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.
 */
function has_filter($tag, $function_to_check = false)
{
    global $wp_filter;
    if (!isset($wp_filter[$tag])) {
        return false;
    }
    return $wp_filter[$tag]->has_filter($tag, $function_to_check);
}

WordPress Version: 4.4

/**
 * Check if any filter has been registered for a hook.
 *
 * @since 2.5.0
 *
 * @global array $wp_filter Stores all of the filters.
 *
 * @param string        $tag               The name of the filter hook.
 * @param callable|bool $function_to_check Optional. The callback to check for. Default false.
 * @return false|int If $function_to_check 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. When using the
 *                   $function_to_check 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.
 */
function has_filter($tag, $function_to_check = false)
{
    // Don't reset the internal array pointer
    $wp_filter = $GLOBALS['wp_filter'];
    $has = !empty($wp_filter[$tag]);
    // Make sure at least one priority has a filter callback
    if ($has) {
        $exists = false;
        foreach ($wp_filter[$tag] as $callbacks) {
            if (!empty($callbacks)) {
                $exists = true;
                break;
            }
        }
        if (!$exists) {
            $has = false;
        }
    }
    if (false === $function_to_check || false === $has) {
        return $has;
    }
    if (!$idx = _wp_filter_build_unique_id($tag, $function_to_check, false)) {
        return false;
    }
    foreach ((array) array_keys($wp_filter[$tag]) as $priority) {
        if (isset($wp_filter[$tag][$priority][$idx])) {
            return $priority;
        }
    }
    return false;
}

WordPress Version: 4.3

/**
 * Check if any filter has been registered for a hook.
 *
 * @since 2.5.0
 *
 * @global array $wp_filter Stores all of the filters.
 *
 * @param string        $tag               The name of the filter hook.
 * @param callback|bool $function_to_check Optional. The callback to check for. Default false.
 * @return false|int If $function_to_check 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. When using the
 *                   $function_to_check 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.
 */
function has_filter($tag, $function_to_check = false)
{
    // Don't reset the internal array pointer
    $wp_filter = $GLOBALS['wp_filter'];
    $has = !empty($wp_filter[$tag]);
    // Make sure at least one priority has a filter callback
    if ($has) {
        $exists = false;
        foreach ($wp_filter[$tag] as $callbacks) {
            if (!empty($callbacks)) {
                $exists = true;
                break;
            }
        }
        if (!$exists) {
            $has = false;
        }
    }
    if (false === $function_to_check || false === $has) {
        return $has;
    }
    if (!$idx = _wp_filter_build_unique_id($tag, $function_to_check, false)) {
        return false;
    }
    foreach ((array) array_keys($wp_filter[$tag]) as $priority) {
        if (isset($wp_filter[$tag][$priority][$idx])) {
            return $priority;
        }
    }
    return false;
}

WordPress Version: 4.0

/**
 * Check if any filter has been registered for a hook.
 *
 * @since 2.5.0
 *
 * @global array $wp_filter Stores all of the filters.
 *
 * @param string        $tag               The name of the filter hook.
 * @param callback|bool $function_to_check Optional. The callback to check for. Default false.
 * @return bool|int If $function_to_check 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. When using the
 *                  $function_to_check 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.
 */
function has_filter($tag, $function_to_check = false)
{
    // Don't reset the internal array pointer
    $wp_filter = $GLOBALS['wp_filter'];
    $has = !empty($wp_filter[$tag]);
    // Make sure at least one priority has a filter callback
    if ($has) {
        $exists = false;
        foreach ($wp_filter[$tag] as $callbacks) {
            if (!empty($callbacks)) {
                $exists = true;
                break;
            }
        }
        if (!$exists) {
            $has = false;
        }
    }
    if (false === $function_to_check || false == $has) {
        return $has;
    }
    if (!$idx = _wp_filter_build_unique_id($tag, $function_to_check, false)) {
        return false;
    }
    foreach ((array) array_keys($wp_filter[$tag]) as $priority) {
        if (isset($wp_filter[$tag][$priority][$idx])) {
            return $priority;
        }
    }
    return false;
}

WordPress Version: 3.9

/**
 * Check if any filter has been registered for a hook.
 *
 * @since 2.5.0
 *
 * @global array $wp_filter Stores all of the filters
 *
 * @param string $tag The name of the filter hook.
 * @param callback $function_to_check optional.
 * @return mixed If $function_to_check 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.
 * 	When using the $function_to_check 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.
 */
function has_filter($tag, $function_to_check = false)
{
    global $wp_filter;
    $has = !empty($wp_filter[$tag]);
    if (false === $function_to_check || false == $has) {
        return $has;
    }
    if (!$idx = _wp_filter_build_unique_id($tag, $function_to_check, false)) {
        return false;
    }
    foreach ((array) array_keys($wp_filter[$tag]) as $priority) {
        if (isset($wp_filter[$tag][$priority][$idx])) {
            return $priority;
        }
    }
    return false;
}

WordPress Version: 3.7

/**
 * Check if any filter has been registered for a hook.
 *
 * @package WordPress
 * @subpackage Plugin
 * @since 2.5
 * @global array $wp_filter Stores all of the filters
 *
 * @param string $tag The name of the filter hook.
 * @param callback $function_to_check optional.
 * @return mixed If $function_to_check 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.
 * 	When using the $function_to_check 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.
 */
function has_filter($tag, $function_to_check = false)
{
    global $wp_filter;
    $has = !empty($wp_filter[$tag]);
    if (false === $function_to_check || false == $has) {
        return $has;
    }
    if (!$idx = _wp_filter_build_unique_id($tag, $function_to_check, false)) {
        return false;
    }
    foreach ((array) array_keys($wp_filter[$tag]) as $priority) {
        if (isset($wp_filter[$tag][$priority][$idx])) {
            return $priority;
        }
    }
    return false;
}