Yogesh Chauhan's Blog

apply_filters function in WordPress

in WordPress on February 20, 2021

In this post, we saw the add_filter hook and how to use that to add functions to your WordPress code.

applt_filters calls the functions that have been added to the filter hook.

Even after adding the functions, the job is not done. We need to call those functions. Those functions attached to the filter hook are invoked by calling applt_filters function.

Syntax


apply_filters($tag, $value )

Where the $tag is required as a string variable — it’s the name of the filter hook.

and the second parameter $value is mixed – these are the values to filter.

We can call applt_filters function to create a new filter hook. To do so, we need to call applt_filters function with the name of the new hook that was specified using the $tag parameter.

Using applt_filters function, we can also pass multiple arguments to hooks.

Example


// The filter callback function.
function concat( $str, $str1, $str2) {
    $str .= $str1 . $str2
    return $str;
}
add_filter( 'concat_filter', 'concat', 10, 3 );

In the example above, concat_filter is the hook. We apply the filters by calling the concat() function that’s hooked onto concat_filter above. We are passing the additional arguments as $str1 and $str2.

The last two parameters in add_filter, 10 and 3 are very important. 10 is the priority and 3 is the number of the parameters.

Credit: WordPress Developers


Most Read

#1 Solution to the error “Visual Studio Code can’t be opened because Apple cannot check it for malicious software” #2 How to add Read More Read Less Button using JavaScript? #3 How to check if radio button is checked or not using JavaScript? #4 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to uninstall Cocoapods from the Mac OS?

Recently Posted

#Apr 8 JSON.stringify() in JavaScript #Apr 7 Middleware in NextJS #Jan 17 4 advanced ways to search Colleague #Jan 16 Colleague UI Basics: The Search Area #Jan 16 Colleague UI Basics: The Context Area #Jan 16 Colleague UI Basics: Accessing the user interface
You might also like these
How to Check If a Variable is an Integer, a Float, a Number, NaN, an Infinite or a Numeric in PHP?PHPfirst-of-type and last-of-type selectors in CSSCSSHow to remove N/A from Radio Button list in Drupal?DrupalHow to show widgets on the Appearance tab in WordPress?WordPressHow to create a new HTML element programmatically using JavaScript?HTMLHow to create two segues with two UIButtons on a single page (Swift 5.0)?Swift