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 <em>actually</em> 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. <em>Note:</em> 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.
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
true
Always returns true.
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.
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.
Signature, return type and hooks compared across 5 parsed releases.
About this page
Parsed data
Generated from the wordpress-develop 6.9.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.