add_filter( string $hook_name, callable $callback, int $priority = 10, int $accepted_args = 1 ): true
- Since
- 0.71
- Source
wp-includes/plugin.php:122
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.
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
- Scaling
- Constant
- Instructions
- 15–19
- Plugin surface
- None
- Called by
- 50
Touches nothing outside its own arguments.
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 19.
Nothing here hands control to plugin code.
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.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 15–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
- Featured_Content::init()Conditionally hooks into WordPress.
- Featured_Content::wp_loaded()Hides "featured" tag from the front end.
- Language_Pack_Upgrader::bulk_upgrade()Upgrades several language packs at once.
- Plugin_Upgrader::bulk_upgrade()Upgrades several plugins at once.
- Plugin_Upgrader::install()Install a plugin package.
- Plugin_Upgrader::upgrade()Upgrades a plugin.
- Theme_Upgrader::bulk_upgrade()Upgrades several themes at once.
- Theme_Upgrader::check_parent_theme_filter()Checks if a child theme is being installed and its parent also needs to be installed.
- Theme_Upgrader::install()Install a theme package.
- Theme_Upgrader::upgrade()Upgrades a theme.
- Twenty_Twenty_One_Custom_Colors::__construct()Instantiates the object.
- Twenty_Twenty_One_Dark_Mode::__construct()Instantiates the object.
Show all 50
- WP_Automatic_Updater::update()Updates an item, if appropriate.
- WP_Comments_List_Table::__construct()Constructor.
- WP_Customize_Custom_CSS_Setting::preview()Add filter to preview post value.
- WP_Customize_Manager::__construct()Constructor.
- WP_Customize_Manager::customize_preview_init()Prints JavaScript settings.
- WP_Customize_Manager::save_changeset_post()Saves the post for the loaded changeset.
- WP_Customize_Manager::start_previewing_theme()If the theme to be previewed isn't the active theme, add filter callbacks to swap it out at runtime.
- WP_Customize_Nav_Menu_Item_Setting::preview()Handle previewing the setting.
- WP_Customize_Nav_Menu_Item_Setting::update()Creates/updates the nav_menu_item post for this setting.
- WP_Customize_Nav_Menu_Setting::preview()Handle previewing the setting.
- WP_Customize_Nav_Menu_Setting::update()Create/update the nav_menu term for this setting.
- WP_Customize_Nav_Menus::__construct()Constructor.
- WP_Customize_Nav_Menus::customize_preview_init()Adds hooks for the Customizer preview.
- WP_Customize_Nav_Menus::customize_register()Adds the customizer settings and controls.
- WP_Customize_Nav_Menus::insert_auto_draft_post()Adds a new `auto-draft` post.
- WP_Customize_Nav_Menus_Panel::render_screen_options()Render screen options for Menus.
- WP_Customize_Setting::__construct()Constructor.
- WP_Customize_Setting::preview()Add filters to supply the setting's value when accessed.
- WP_Customize_Widgets::__construct()Initial loader.
- WP_Customize_Widgets::capture_filter_pre_update_option()Pre-filters captured option values before updating.
- WP_Customize_Widgets::customize_register()Registers Customizer settings and controls for all sidebars and widgets.
- WP_Customize_Widgets::override_sidebars_widgets_for_theme_switch()Override sidebars_widgets for theme switch.
- WP_Customize_Widgets::render_widget_partial()Renders a specific widget using the supplied sidebar arguments.
- WP_Customize_Widgets::selective_refresh_init()Adds hooks for selective refresh.
- WP_Customize_Widgets::start_capturing_option_updates()Begins keeping track of changes to widget options, caching new values.
- WP_Embed::__construct()Constructor
- WP_Importer::get_page()Gets URL.
- WP_Interactivity_API::add_hooks()Adds the necessary hooks for the Interactivity API.
- WP_List_Table::__construct()Constructor.
- WP_Locale_Switcher::init()Initializes the locale switcher.
- WP_Media_List_Table::display_rows()Generates the list table rows.
- WP_Metadata_Lazyloader::queue_objects()Adds objects to the metadata lazy-load queue.
- WP_Posts_List_Table::display_rows()Generates the list table rows.
- WP_REST_Attachments_Controller::create_item()Creates a single attachment.
- WP_REST_Attachments_Controller::create_item_from_url()Sideloads an external image from a URL into the media library.
- WP_REST_Attachments_Controller::prepare_items_query()Determines the allowed query_vars for a get_items() response and prepares for WP_Query.
- WP_REST_Attachments_Controller::sideload_item()Side-loads a media file without creating a new attachment.
- WP_REST_Comments_Controller::check_read_post_permission()Checks if the post can be read.
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.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 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.