So you have your code in a class, let’s say it looks like this…
<?php
class WPSE82245
{
public function action_init()
{
// do stuff
}
}
Now you try to hook it into something…
add_action('init', 'array('WPSE82245', 'action_init'));
What happens is when the init
hook fires, WordPress tries to call your method. It would similar to if you just wrote this…
WPSE82245::action_init();
But PHP doesn’t like that because you haven’t declare you method static
, that is, able to be used without an instance of its container class (like the example directly above).
This makes sense, what if you used $this
in your method? Calling it statically would cause a runtime error saying that $this
was being used outside an object context.
You can remove the errors by declaring your method static…
<?php
class WPSE82245
{
public static function action_init()
{
// do stuff
}
}
Or using an instance of your class as the first element of the array the callable argument of add_action
.
<?php
class WPSE82245
{
public function action_init()
{
// do stuff
}
}
$cls = new WPSE82245();
add_action('init', array($cls, 'action_init'));
The above is just an example there are many different ways to instantiate a class in a WordPress theme/plugin.