0

I'm working with Ultimate Member plugin and while attempting to work through a modification I accidentally used add_action on a filter hook (not an action hook) and it still worked - as in the call back ran??

Is this a feature of Wordpress I was not aware of? I don't see any documentation about this.

below is the code I ran. N.B no return either.

Here is a link to the UM docs about this filter hook um_set_user_role

I did a global search on all files in the Ultimate Member plugin and it is definitely not called with do_action anywhere.. so how did this work?


add_action( 'um_set_user_role', 'add_user_specific_role', 99, 3 );
function add_user_specific_role( $role, $user_id, $user ) {

    error_log( '============= UM NEW ROLE =================' );
    error_log($role);
    error_log( '============= UM USER ALL ROLES =================' );
    error_log(print_r($user->roles, true));

}

1 Answer 1

1

add_action() and add_filter() are essentially the same thing, hooks, and they both call the same functions under the hood. If you look you'll see WP_Hook::do_action() basically just calls WP_Hook::apply_filters().

An action is essentially just a filter that doesn't return a value. If your callback function was supposed to modify the value of $role then that part wouldn't work, because add_action() won't pass the value through, but the callback will still run, as you've seen.

1
  • Thanks, I should have looked at the source instead of the notes about it. I've just learnt something new! Incidentally, while you are correct, the core of it is that add_action just calls (or returns) add_filter under the hood, rather than anything do_action does since that isn't called anywhere in this scenario.
    – mike_temby
    Mar 29, 2022 at 5:39

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.