0

I'd think you can't. But I come across this code in a plugin's file:

add_action( 'render_block', array( $this, 'render_block' ), 5, 2 );
public function render_block( $block_content, $block ) {
.....

A brief search shows that render_block is a filter https://developer.wordpress.org/reference/hooks/render_block/

I put var_dump(debug_backtrace()) inside the render_block function above and find out the invoker of this function is in line 309 of class-wp-hook.php, which is inside apply_filter function.

enter image description here

enter image description here

So I am little confused. Did I miss anything here?

1
  • 1
    Filters are hooks that expect a value to be returned. You could run the filter as an action, but the callback would still expect the passed value to be returned. Otherwise, PHP will throw out notices and your website may not function as expected. I can't think of any good reason why you would want to call a filter hook as an action hook - this would be awfully confusing.
    – Howdy_McGee
    Oct 26, 2022 at 21:50

1 Answer 1

0

add_action() and add_filter() are the same function. If you look at the source you'll see that add_action() just calls add_filter(). So yes, this it is technically possible to register a filter callback with add_action().

The difference between an action and a filter is that filters expect a return value, while actions do not. This is important when defining the hook with do_action() and apply_filters() because do_action() does not return a value. When it comes to registering a callback the only difference between add_filter() and add_action() is the name, but it's a good idea to use the right function so that it's clear what your code is doing. But if you make a mistake and use the wrong one, as this plugin has, it's not the end of the world.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.