WordPress Action hooks must be added in Main Class.
There are 3 ways to add and handle actions at Main Class:
In the example above, action save_post
is being handled by controller PostController
(controllers\PostController.php
) and its method called save
.
Notice how add_action
is being called with $this
(referencing your Main Class); this must be like this because we want the Main Class to handle the action with MVC.
We can override parameters passed by to the controller like this:
In the example above, action admin_menu
is being handled by controller AdminController
(controllers/AdminController.php
) and its method called menu
.
The 3rd parameter is an array of parameters we want to pass to the controller. The first one is an integer of value 5 and the second one is the configuration object of the Main Class.
You can also set the priority and accepted arguments like you normally would in WordPress add_action function:
In the example above, action ajax_logo
will render view images.logo
(views/images/logo.php
).
Notice how add_action
is being called with $this
(referencing your Main Class); this must be like this because we want the Main Class to handle the action with MVC.
We can pass by view parameters like this:
In the example above, action ajax_logo
will render view images.logo
(views/images/logo.php
).
The 3rd parameter is an array with the variables and values passed to the view.
Legacy means adding actions as you normally would in WordPress:
Notice how we are passing the Main Class object as reference in the add_action
function and $this->add_action
is not longer needed.