Title: WP_Hook::add_filter
Published: December 6, 2016
Last modified: February 24, 2026

---

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

## In this article

 * [Parameters](https://developer.wordpress.org/reference/classes/wp_hook/add_filter/?output_format=md#parameters)
 * [Source](https://developer.wordpress.org/reference/classes/wp_hook/add_filter/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/classes/wp_hook/add_filter/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/classes/wp_hook/add_filter/?output_format=md#changelog)

[ Back to top](https://developer.wordpress.org/reference/classes/wp_hook/add_filter/?output_format=md#wp--skip-link--target)

Adds a callback function to a filter hook.

## 󠀁[Parameters](https://developer.wordpress.org/reference/classes/wp_hook/add_filter/?output_format=md#parameters)󠁿

 `$hook_name`stringrequired

The name of the filter to add the callback to.

`$callback`callablerequired

The callback to be run when the filter is applied.

`$priority`intrequired

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`intrequired

The number of arguments the function accepts.

## 󠀁[Source](https://developer.wordpress.org/reference/classes/wp_hook/add_filter/?output_format=md#source)󠁿

    ```php
    public function add_filter( $hook_name, $callback, $priority, $accepted_args ) {
    	if ( null === $priority ) {
    		$priority = 0;
    	}

    	$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' => (int) $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 );
    	}

    	$this->priorities = array_keys( $this->callbacks );

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

[View all references](https://developer.wordpress.org/reference/files/wp-includes/class-wp-hook.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-includes/class-wp-hook.php#L82)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-includes/class-wp-hook.php#L82-L106)

## 󠀁[Related](https://developer.wordpress.org/reference/classes/wp_hook/add_filter/?output_format=md#related)󠁿

| Uses | Description | 
| [WP_Hook::resort_active_iterations()](https://developer.wordpress.org/reference/classes/wp_hook/resort_active_iterations/)`wp-includes/class-wp-hook.php` |

Handles resetting callback priority keys mid-iteration.

  | 
| [_wp_filter_build_unique_id()](https://developer.wordpress.org/reference/functions/_wp_filter_build_unique_id/)`wp-includes/plugin.php` |

Builds a unique string ID for a hook callback function.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/classes/wp_hook/add_filter/?output_format=md#changelog)󠁿

| Version | Description | 
| [4.7.0](https://developer.wordpress.org/reference/since/4.7.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fclasses%2Fwp_hook%2Fadd_filter%2F)
before being able to contribute a note or feedback.