WordPress Actions Vs Filters

Both actions and filters are WordPress hooks that allow you to hook into certain code within WordPress core or a plugin.

Actions allow you to add code at a point where WordPress, a plugin or your own custom action is doing something. There will need to be a do_action called so that you can hook into

do_action( string $hook_name, mixed $arg )

If there is an action available you can then use add_action to hook into that point

// The action callback function.
function example_callback( $arg1, $arg2 ) {
    // (maybe) do something with the args.
}
add_action( 'example_action', 'example_callback', 10, 2 );

Filters pass in data that you are to modify and then return. There will need to be apply_filters with code to hook into.

$value = apply_filters( 'example_filter', 'filter me', $arg1, $arg2 );
function example_callback( $example ) {
    // Maybe modify $example in some way.
    return $example;
}
add_filter( 'example_filter', 'example_callback' );

Leave a comment

Your email address will not be published.

For security, use of Google's reCAPTCHA service is required which is subject to the Google Privacy Policy and Terms of Use.

I agree to these terms.