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
- CALLBACK_TYPE_MODAL = 0;
- CALLBACK_TYPE_STRING = 1;
- CALLBACK_TYPE_TEMPLATE = 2;
- CALLBACK_TYPE_FILE = 3;
- CALLBACK_TYPE_DATA = 4;
- CALLBACK_TYPE_SCRIPT = 5;
- CALLBACK_TYPE_MESSAGE = 6;
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:
ActionTypeCallbackis an abstract class, not an interface, so youextendsit and callparent::__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— returntrueto allow logged-out access via thewp_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.nullkeeps the legacy logged-in-only behaviour.
Combine both for fine-grained access on top of Groups.
