I wasn't sure if this was a PHP issue (suited for StackOverflow) or a WordPress issue (suited for StackExchange), however since my issue appears to be with the add_action()
function, I have placed the question here.
Here's some simplified code which causes the same issue (to avoid re-posting my couple-hundred line plugin):
<?php
$class = new MyClass();
add_action('init', array($class, 'init'));
class MyClass {
public static function init() {
$this->core();
}
public static function core() {
echo "I never get this far...";
}
}
?>
I get the following error:
Fatal error: Using $this when not in object context in /home/coupon/public_html/wp-content/plugins/test.php on line 7
If my add_action()
call were instead:
add_action('init', 'DEMenu::init');
Then I would understand this error (since the function was called statically and there is no instance of DEMenu for $this
to point to), however I don't understand why this is happening since I used an array and passed an instance of the class.
My specific issue is actually related to this problem. I created a custom Walker class then used the wp_nav_menu_args
filter to pass my custom Walker. I get this error in class-wp-walker.php
on line 185. The line in particular is:
$id_field = $this->db_fields['id'];
Any ideas?
Task: Call a class method as a hook for a WordPress plugin so that the $this
variable is not broken