WP_Hook::add_filter() WordPress Method

The add_filter() method is used to add a new filter to a WordPress hook. This is a powerful method which can be used to change the behavior of a WordPress site.

WP_Hook::add_filter( string $hook_name, callable $callback, int $priority, int $accepted_args ) #

Adds a callback function to a filter hook.


Parameters

$hook_name

(string)(Required)The name of the filter to add the callback to.

$callback

(callable)(Required)The callback to be run when the filter is applied.

$priority

(int)(Required)The order in which the functions associated with a particular filter are executed. Lower numbers correspond with earlier execution, and functions with the same priority are executed in the order in which they were added to the filter.

$accepted_args

(int)(Required)The number of arguments the function accepts.


Top ↑

Source

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

	public function add_filter( $hook_name, $callback, $priority, $accepted_args ) {
		$idx = _wp_filter_build_unique_id( $hook_name, $callback, $priority );

		$priority_existed = isset( $this->callbacks[ $priority ] );

		$this->callbacks[ $priority ][ $idx ] = array(
			'function'      => $callback,
			'accepted_args' => $accepted_args,
		);

		// If we're adding a new priority to the list, put them back in sorted order.
		if ( ! $priority_existed && count( $this->callbacks ) > 1 ) {
			ksort( $this->callbacks, SORT_NUMERIC );
		}

		if ( $this->nesting_level > 0 ) {
			$this->resort_active_iterations( $priority, $priority_existed );
		}
	}


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.