Actions

WordPress Action hooks must be added in Main Class.

There are 3 ways to add and handle actions at Main Class:

  1. With Controllers.
  2. With Views.
  3. Legacy callback.

With Controllers

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.

Override parameters

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.

Priority and accepted arguments

You can also set the priority and accepted arguments like you normally would in WordPress add_action function:

With Views

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.

View parameters

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

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.