WP_Hook::build_preinitialized_hooks() WordPress Method

The build_preinitialized_hooks() method is used to pre-initialize hooks. This is useful for when you need to ensure that a particular hook is always available, even if it's not currently registered.

WP_Hook::build_preinitialized_hooks( array $filters ) #

Normalizes filters set up before WordPress has initialized to WP_Hook objects.


Description

The $filters parameter should be an array keyed by hook name, with values containing either:

  • A WP_Hook instance
  • An array of callbacks keyed by their priorities

Examples:

$filters = array(
    'wp_fatal_error_handler_enabled' => array(
        10 => array(
            array(
                'accepted_args' => 0,
                'function'      => function() {
                    return false;
                },
            ),
        ),
    ),
);

Top ↑

Parameters

$filters

(array)(Required)Filters to normalize. See documentation above for details.


Top ↑

Return

(WP_Hook[]) Array of normalized filters.


Top ↑

Source

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

	public static function build_preinitialized_hooks( $filters ) {
		/** @var WP_Hook[] $normalized */
		$normalized = array();

		foreach ( $filters as $hook_name => $callback_groups ) {
			if ( is_object( $callback_groups ) && $callback_groups instanceof WP_Hook ) {
				$normalized[ $hook_name ] = $callback_groups;
				continue;
			}

			$hook = new WP_Hook();

			// Loop through callback groups.
			foreach ( $callback_groups as $priority => $callbacks ) {

				// Loop through callbacks.
				foreach ( $callbacks as $cb ) {
					$hook->add_filter( $hook_name, $cb['function'], $priority, $cb['accepted_args'] );
				}
			}

			$normalized[ $hook_name ] = $hook;
		}

		return $normalized;
	}


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.