WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have created a class from within which I would like to call a private method (from within the same class) from the __construct method as an action callback.

When I would like to use a public method I can access it by:

add_action( 'init', array( $this, 'action_callback' ) );

However, this causes an error when the method is private. I have also tried unsuccessfully:

add_action('init', $this->action_callback() );

How do I access a private method?

The class looks something like:

class My_class {
    public function __construct() {
        add_action( 'init', array( $this, 'action_callback' ) );

    private function action_callback() {
        // do something
share|improve this question
The best you could do is add @access private to the method doc, and also add an underscore before the method. Both of these will tell any experienced programmer that they should NOT be using the method, as it isn't intended to be public. – Joel Worsham Jul 1 '15 at 11:51
up vote 7 down vote accepted

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.

share|improve this answer
Very clear explanation, thanks – tommyf Jun 26 '14 at 6:56

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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