Every WordPress developer should be using Classes for their plugins and extensions. But how?
This is just a mini-post about how to call your class extensions using hooks, simply because I had trouble finding this information out myself on the interwebs.
Firstly, build plugins using classes – I won’t go into this because there are (at least) two decent introductions to it here and here.
There is only one thing I need to add which were not included in these articles – How to reference / call your class methods from the WP templates. One answer is: add_action and do_action() wordpress hooks.
Here’s how it works:
class myClassName { function __construct() { add_action('MY_HOOK_NAME', array($this, 'MY_HOOK_FUNCTION')); } function MY_HOOK_FUNCTION() { // FUNCTION HERE } } // class
Then in your template file you call your class action with this:
do_action('MY_HOOK_NAME');
And that’s it. Hope it helps!