Codex

Function Reference/add filter

Contents

Description

Hooks a function to a specific filter action.

Filters are the hooks that WordPress launches to modify text of various types before adding it to the database or sending it to the browser screen. Plugins can specify that one or more of its PHP functions is executed to modify specific types of text at these times, using the Filter API. See the Plugin API for a list of filter hooks.

Usage

 <?php add_filter$tag$function_to_add$priority$accepted_args ); ?> 

Parameters

$tag
(string) (required) The name of the filter to hook the $function_to_add to.
Default: None
$function_to_add
(callback) (required) The name of the function to be called when the filter is applied.
Default: None
$priority
(integer) (optional) Used to specify the order in which the functions associated with a particular action 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 action.
Default: 10
$accepted_args
(integer) (optional) The number of arguments the function(s) accept(s). In WordPress 1.5.1 and newer. hooked functions can take extra arguments that are set when the matching do_action() or apply_filters() call is run.
Default: 1

Return

The function returns true whether the attempted function hook fails or not. There is no test that the function exists nor whether the $function_to_add is even a string. It is up to you to take care and this is done for optimization purposes, so everything is as quick as possible.

Example

Notes

You may need to supply a pointer to the function's namespace for some filter callbacks, e.g.

 <?php add_filter('media_upload_newtab', array(&$this'media_upload_mycallback')); ?> 

Otherwise WordPress looks in its own namespace for the function, which can cause abnormal behaviour.

In WordPress 1.5.1+, hooked functions can take extra arguments that are set when the matching do_action() or apply_filters() call is run. For example, the action comment_id_not_found will pass any functions that hook onto it the ID of the requested comment.

Change Log

Since: 0.71

Source File

add_filter() is located in wp-includes/plugin.php.

Related

Filters: add_filter, has_filters, apply_filters, remove_filter, remove_all_filters, current_filter, merge_filters

See also index of Function Reference and index of Template Tags.
This article is marked as in need of editing. You can help Codex by editing it.