I'm a beginner for WordPress. I want to know what are the differences between actions and filters in WordPress.
Thank you.
I'm a beginner for WordPress. I want to know what are the differences between actions and filters in WordPress.
Thank you.
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.