Is it possible to use object in add_action?

You can do this:

class testclass {
  function test() {
    echo 'howdy';
  }
}
add_action('wp_head',array('testclass','test'));

Or this:

$t = new testclass();
add_action('wp_head',array($t,'test'));

It doesn’t work like…

$t = new testclass();
add_action('wp_head','t');
// or this either, for good measure
$t = new testclass();
add_action('wp_head',array('t'));

.. but I am not sure what you are trying to accomplish by using that pattern. You’ve already instantiated the class so the constructor, if present, has already ran. Without a callback method, I don’t know what you expect to happen.

Leave a Comment