2

I'm a beginner for WordPress. I want to know what are the differences between actions and filters in WordPress.

Thank you.

  • This explains everything: [Actions and filters are NOT the same thing… » Otto on WordPress](http://ottopress.com/2011/actions-and-filters-are-not-the-same-thing/) – brasofilo Oct 23 '18 at 11:48
  • I found the duplicates using this google query: `actions filters wordpress site:stackoverflow.com` – brasofilo Oct 23 '18 at 11:50

1 Answers1

0

An action in WordPress allows you to add a piece of code when a specific event runs. For example

//Fires as an admin screen or script is being initialized (admin_init) hook.
add_action("admin_init", "add_some_code" );
function add_some_code(){
// You can add any code here and it will be executed
}

A filter is very much the same, except it's main purpose is to be used to modify a variable instead of injecting code.

//function to modify some variable
function example_callback( $value ) {
//logic here
return $modified_value;
}
//add filter to run on some event (hook), function to run, priority, and $someValue as the value to modify.
add_filter( 'hook', 'example_callback', 10, $someValue ); 

You can read more about actions and filters with examples here.

BambiOurLord
  • 372
  • 5
  • 12