Using a private method as an action callback from within a class

It’s not possible to call a private method through an action or filter. When calling add_action or add_filter, WordPress adds the callback to a list of callbacks for that specific action or filter. Then, when do_action or apply_filters is called, WordPress uses call_user_func_array to call the linked functions and methods. As call_user_func_array is not called from within the class, it can’t call private methods of that class.

Furthermore, there’s no proper way to really keep the method private, even though you could add a separate (public) method to your class, add that as a callback to the action, and have that call the private method. In doing that, however, you lose the real concept of the method being private.

Leave a Comment