WordPress at a glance

add_filter() WP 0.71

Attaches the specified PHP function to the specified filter hook. Thus, when the filter is triggered, the value will be filtered by the specified PHP function.

A filter is a hook inside code through which you can "filter" any data. For example, during receiving and displaying some text from the database, you can change it "on the fly" and display the already modified version of the text. Thanks to the filters, for doing such text change we do not have to edit code in WP core files, but we can do it in our plugin or theme php file.

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.

This function does not make any checks: whether there is an attached function or whether the function name is passed as a string, etc.. This is to make add_filter() work as fast as possible.

  • Global. Array. $wp_filter A multidimensional array of all hooks and the callbacks hooked to them.
Is the basis for: add_action()
✈ 1 time = 0.000015s = very fast | 50000 times = 0.05s = speed of light | PHP 7.0.8, WP 4.7

No Hooks.

Return

true.

Usage

add_filter( $tag, $function_to_add, $priority, $accepted_args );
$tag(string) (required)
The name of the filter to hook the $function_to_add callback to.
$function_to_add(callable) (required)
The callback to be run when the filter is applied.
$priority(int)
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(int)
The number of arguments the function accepts.
Default: 1

Examples

#1 A common example that shows how you can add any label to the end of each article through the_content filter:

add_filter( 'the_content', 'add_text_to_content' );
function add_text_to_content($content){
	$out = $content . "<p>When copying an article, put a back link please!</p>";
	return $out;
}

#2 Modify the "pictures signature" block (сaption)

Example of changing the caption of images () for HTML 5. The function hooks on the img_caption_shortcode filter, which is responsible for converting the shortcode in the article content. To get all 3 parameters passed by the filter, specify the argument $accepted_args = 3:

add_filter( 'img_caption_shortcode', 'my_img_caption_shortcode_filter', 10, 3 );
/**
 * The filter replaces the  shotcode under HTML5 standards
 *
 * @return string HTML Text describing the <figure> tag
 **/
function my_img_caption_shortcode_filter( $val, $attr, $content = null ){
	extract(shortcode_atts(array(
		'id'    => '',
		'align' => '',
		'width' => '',
		'caption' => ''
	), $attr));

	if ( 1 > (int) $width || empty($caption) )
		return $val;

	$capid = '';
	if ( $id ) {
		$id = esc_attr($id);
		$capid = 'id="figcaption_'. $id . '" ';
		$id = 'id="' . $id . '" aria-labelledby="figcaption_' . $id . '" ';
	}

	return '<figure ' . $id . 'class="wp-caption ' . esc_attr($align) . '" style="width: '
	. (10 + (int) $width) . 'px">' . do_shortcode( $content ) . '<figcaption ' . $capid 
	. 'class="wp-caption-text">' . $caption . '</figcaption></figure>';
}

#3. The function can be passed as anonymous:

Don't use an anonymous function as a callback function if you intend to use filter many times. Also, remember that it's impossible to remove filter declared with an anonymous function.

add_filter( 'the_title', function( $title ){
	return '<b>'. $title. '</b>';
});

#4. Inside the classes, the filter function must be specified through an array:

The first value of the array is an instance of the class and the second is a name of the method of this class.

add_filter( 'media_upload_newtab', array( $this, 'media_upload_mycallback') );

If the method is static, the first value of the array must be specified as name of the class:

add_filter( 'media_upload_newtab', array( 'My_Class', 'media_upload_mycallback') );

Code of add_filter: wp-includes/plugin.php VER 5.0.1

<?php
function add_filter( $tag, $function_to_add, $priority = 10, $accepted_args = 1 ) {
	global $wp_filter;
	if ( ! isset( $wp_filter[ $tag ] ) ) {
		$wp_filter[ $tag ] = new WP_Hook();
	}
	$wp_filter[ $tag ]->add_filter( $tag, $function_to_add, $priority, $accepted_args );
	return true;
}

Related Functions

From tag: Filters (hooks)

More from category: Hooks (actions, filters)

No comments
    Hello, !     Log In . Register