Yogesh Chauhan's Blog

add_filter function in WordPress

in WordPress on February 17, 2021

We can use add_filter to hook a function or a method to a specific filter action.

WordPress has tons of functions to make changes in the website – backend and frontend. WordPress also has filter hooks.

filter hooks 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 we apply the filter, each bound callback is run in order of priority.

Syntax


add_filter($tag, $function_to_add, $priority = 10, $accepted_args = 1 )

Where,

$tag is string and required. It’s the name of the filter to hook.

$function_to_add is required as well. It is callable. The callback to be run when the filter is applied. You can also pass a class method as a callback.

$priority is optional but if we want to pass it then it has to be an integer. The default value is 10.

$priority is used to specify the order in which the functions associated with a particular action are executed. Lower numbers means it will execute first. If two functions have same priorities then they will be executed in the order in which they were added to the action.

$accepted_args – as the name suggests – It’s the number of arguments the function accepts. It is optional but if we want to pass it then it has to be an integer. The default value is 1. It makes sense! The function will always accept at least one argument.

Hooked functions can take extra arguments that are set when the matching do_action() or apply_filters() call is run.

Examples

Simple callback



function square( $var ) {
    return $var*$var;
}
add_filter( 'square_filter', 'square' );


Change the frontpage sections



add_filter( 'twentytwentyone_front_page_sections', 'prefix_custom_front_page_sections' );
function prefix_custom_front_page_sections( $num_sections )
{
  return 6;
}


Display custom length of post excerpt



if( ! function_exists( 'prefix_custom_excerpt_length' ) )
{
    function prefix_custom_excerpt_length( $length )
    {
        return 40;
    }
}
add_filter( 'excerpt_length', 'prefix_custom_excerpt_length', 999 );



Inject a CLASS/ID CSS in content



//Add Class/ID to Post Content
add_filter('the_content', 'xai_my_class');
function xai_my_class($content)
{
    //Replace the instance with the Class/ID markup.
    $string = '<ul'; //your tag
    $replace = '<ul class="detail-list"'; //add your class/id and tag
    $content = str_replace( $string, $replace, $content );
    return $content;
}



Credit goes to: WordPress Official Docs


Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #3 How to add Read More Read Less Button using JavaScript? #4 How to uninstall Cocoapods from the Mac OS? #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to Use SQL MAX() Function with Dates?

Recently Posted

#Aug 15 Is PHP still good for back-end programming? #Aug 10 How to create a multisite network in WordPress? #Aug 3 How to create a circle that follows a cursor using JavaScript and CSS? #Aug 3 How to make a curtain slider using jQuery and CSS? #Aug 2 How to progressively load images and add a blurry placeholder? #Aug 1 How to create a placeholder loader (throbber) using CSS?
You might also like these
Did you get an error while displaying Special Characters in JavaScript?JavaScriptSelector Lists and Combinators in SCSS (Sass)SCSSLearn to create your skill bar using CSSCSSHow to add Local State to a Class in React?ReactHow to obfuscate JavaScript code to hide it from View Source?JavaScriptHow to Secure Web or Mobile Browsers?Miscellaneous