Actions

Wonderful Relations Actions are the default way to register AJAX endpoints. The client triggers a standardized wr_ajax_process_entry request, the server resolves the registered ActionType, and the matching ActionTypeCallback returns a typed result that the frontend knows how to render.

Every callback returns an array with a fixed shape:

return array( $callback_type, $payload );

The callback type tells action.js how to interpret $payload (open a modal, swap a DataTable, download a file, …).

Callback Types

Minimal Implementation

namespace TS\YourPlugin\Modules\Hello\Actions;
 
use TS\WonderfulRelations\Includes\Constants;
use TS\WonderfulRelations\System\Action\ActionType\ActionType;
use TS\WonderfulRelations\System\Action\ActionType\ActionTypeCallback;
 
class ReturnText extends ActionTypeCallback {
 
    public function __construct() {
        parent::__construct();
        new ActionType( "your_plugin_return_text", $this );
    }
 
    public function execute_callback(): array {
        return array( Constants::CALLBACK_TYPE_STRING, "TEXT" );
    }
}
 
new ReturnText();

Note the public surface: ActionTypeCallback is an abstract class, not an interface, so you extends it and call parent::__construct(). ActionType::__construct() only takes two arguments: the registered action name and the callback. The callback type is part of the array the callback returns, not the constructor.

The ActionType constructor hooks itself into the register_new_action_type WordPress action, so simply instantiating it once during plugin boot is enough to make the action reachable from the frontend.

Example Callback: Reload a DataTable

return array(
    Constants::CALLBACK_TYPE_SCRIPT,
    "jQuery('#{$this->payload['datatable_identifier']}').DataTable().ajax.reload()"
);

Capability and Public-Access Surface

ActionTypeCallback exposes two opt-in hooks on every callback:

  • allow_nopriv(): bool — return true to allow logged-out access via the wp_ajax_nopriv_ route. Default-deny applies; only mark callbacks public when they truly are read-only.
  • required_capability(): ?string — return a WordPress capability that must be present before the callback runs. null keeps the legacy logged-in-only behaviour.

Combine both for fine-grained access on top of Groups.