wppaste
WordPress

add_filter( string $hook_name, callable $callback, int $priority = 10, int $accepted_args = 1 ): true

Since
0.71
Source
wp-includes/plugin.php:121

Register a callback on a WordPress filter hook with add_filter() to modify a value, such as post content or a title, before WordPress uses it. Callbacks receive the value, must return it, and run in priority order; set $accepted_args when your callback takes more than one argument. Returns true whether or not the callback is valid.

Adds a callback function to a filter hook.

Description

WordPress offers filter hooks to allow plugins to modify various types of internal data at runtime.

A plugin can modify data by binding a callback to a filter hook. When the filter is later applied, each bound callback is run in order of priority, and given the opportunity to modify a value by returning a new value.

The following example shows how a callback function is bound to a filter hook.

Note that $example is passed to the callback, (maybe) modified, then returned:

function example_callback( $example ) {
 // Maybe modify $example in some way.
 return $example;
}
add_filter( 'example_filter', 'example_callback' );

Bound callbacks can accept from none to the total number of arguments passed as parameters in the corresponding apply_filters() call.

In other words, if an apply_filters() call passes four total arguments, callbacks bound to it can accept none (the same as 1) of the arguments or up to four. The important part is that the $accepted_args value must reflect the number of arguments the bound callback actually opted to accept. If no arguments were accepted by the callback that is considered to be the same as accepting 1 argument. For example:

// Filter call.
$value = apply_filters( 'hook', $value, $arg2, $arg3 );

// Accepting zero/one arguments.
function example_callback() {
 ...
 return 'some value';
}
add_filter( 'hook', 'example_callback' ); // Where $priority is default 10, $accepted_args is default 1.

// Accepting two arguments (three possible).
function example_callback( $value, $arg2 ) {
 ...
 return $maybe_modified_value;
}
add_filter( 'hook', 'example_callback', 10, 2 ); // Where $priority is 10, $accepted_args is 2.

Note: The function will return true whether or not the callback is valid.
It is up to you to take care. This is done for optimization purposes, so everything is as quick as possible.

Compatibility

WordPress
since 0.71
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$hook_namestring
The name of the filter to add the callback to.
$callbackcallable
The callback to be run when the filter is applied.
$priorityintoptional
Used to specify 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. Default 10.Default: 10
$accepted_argsintoptional
The number of arguments the function accepts. Default 1.Default: 1

Return value

true
Always returns true.

Code examples

Every example is editable and runs in a real WordPress booted in your browser by WordPress Playground. Press Run, then edit the code: clicking away re-runs it. Nothing is sent anywhere until you do.

Append markup to post content

Filter the_content to add a note after every single post on the front end.

add_filter( 'the_content', 'myplugin_append_notice' );

function myplugin_append_notice( $content ) {
	if ( is_singular( 'post' ) && in_the_loop() && is_main_query() ) {
		$content .= '<p class="reading-note">' . esc_html__( 'Thanks for reading!', 'myplugin' ) . '</p>';
	}

	return $content;
}

Always return $content, even when you change nothing; a filter callback that returns nothing blanks the value for everything downstream.

Accept extra arguments from the hook

Take the second argument the_title passes ($post_id) by declaring $accepted_args as 2.

add_filter( 'the_title', 'myplugin_flag_private_titles', 10, 2 );

function myplugin_flag_private_titles( $title, $post_id ) {
	if ( 'private' === get_post_status( $post_id ) ) {
		$title = '[Members] ' . $title;
	}

	return $title;
}

The fourth argument must reflect how many parameters the callback actually accepts; leave it at the default 1 and WordPress calls the function with only $title, so $post_id is missing and PHP fatals.

Performance profile

How much work a call to add_filter() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Trivial

Touches nothing outside its own arguments.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
15–19

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 19.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
50

50 places in core call this, so the cost is paid more often than your own code shows.

What one call costs · 1 distinct outcome

One number would be a lie: the work depends on which branch runs. These are every distinct cost add_filter() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always15–19->add_filter()

Across PHP versions

Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 19 instructions, 15–19 executed per call, 1 branch. The work does not change between versions.

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Uses · 1

  • WP_Hook::__construct()

Used by · 50

Show all 50

Source code

function add_filter( $hook_name, $callback, $priority = 10, $accepted_args = 1 ) {	global $wp_filter; 	if ( ! isset( $wp_filter[ $hook_name ] ) ) {		$wp_filter[ $hook_name ] = new WP_Hook();	} 	$wp_filter[ $hook_name ]->add_filter( $hook_name, $callback, $priority, $accepted_args ); 	return true;}

Changelog

Introduced in 0.71. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 6.7.7 tag, from src/wp-includes/plugin.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.