Skip to main content Link Menu Expand (external link) Document Search Copy Copied

Action

One of the most important part of plugin development in WordPress is Actions.

You can add actions in init & onAdmin methods.

addAction method

addAction method is defined as so:

public function addAction(string $hook, array $callback, int $priority = 10, int $accepted_args = 1)
    {
        $this->validateCallback($callback);
        $this->actions[] = $this->getHook($hook, $callback, $priority, $accepted_args);
    }

You should use this method like so:

$this->addAction();

addAction parameters

$hook & $priority & $accepted_args does not need any explanation.

But for callbacks you need to create a controller first.

You can create your controllers in ./app/Controllers. controller is a class that you write your logic in it.

after creating you class in given folder you should add it under this namespace: namespace Realtyna\YOUR-PLUGIN-NAMESPACE\Controllers;

After creating your controller class for example UserController, create a public method in it, for example: index

now you can register your action like so:

$this->addAction('example-hookname', [UserController::class, 'index'], 5, 1);

You can also create sub folders for your controllers but be careful about their namespaces. for example, you create your controller like so:

./app/Controllers/Admin/Pages/ExampleController.php

so your namespace will be: namespace Realtyna\YOUR-PLUGIN-NAMESPACE\Controllers\Admin\Pages;